下面会结合例子,在说明组内取值的同时,是如何使用相关子查询的。
在MySQL中查询每组中的前N个值
有如上的表,我们需要找出每个类型(type)中最便宜的前两种水果,我们可以采取这样的方法:
select type, variety, pricefrom fruitswhere (select count(*) from fruits as fwhere f.type = fruits.type and f.price < fruits.price) <= 1;
这个查询语句就使用到了相关子查询,结合之前我们提到的相关子查询的查询步骤,我们来解析一下这个SQL语句。
从表中首先取出第一条数据,type为apple,variety为gala,price为2.79。相关子查询中,会将元组相关列的值传给内层查询,在子查询中我们看到
select count(*) from fruits as fwhere f.type = fruits.type and f.price < fruits.price
也就是说,实际上这条语句此时应该是这样
即,在apple这个类别中,价格比2.79还要低的元组的数量。
select count(*) from fruits as fwhere f.type = apple and f.price < 2.79
我们再看全貌
可以知道,apple中价格低于2.79的只有fuji这个元组,即值为1,满足子条件<=1的条件,所以该字段会被输出。
select type, variety, pricefrom fruitswhere (select count(*) from fruits as fwhere f.type = apple and f.price < 2.79) <= 1;
如果不好理解,这样来看。其实这个子条件的意思就是在说,我提取一个元组A,如果这个元组A所属的类别中,价格比元组A低的最多只有一个,那么就输出(即它本身是最便宜的,那么比它价格还低的就只有0个;或者它是第二便宜的,那么价格比它低的就只有1个。这样一来,只要数量条件为<=1,那么就得到了最便宜的两种水果)
按照相关子查询的执行顺序,第一个元组符合条件,被输出。接下来,把第二个元组的值带入,如果符合条件则输出,接着第三个元组第四个元组... 直到所有的元组带入筛选完毕,整个查询过程也就结束了。
什么,你说价格排列没有顺序?加上order就可以了。
select type, variety, pricefrom fruitswhere (select count(*) from fruits as fwhere f.type = fruits.type and f.price < fruits.price) <= 1order by fruits.type, fruits.price
参考链接
- How to select the first/least/max row per group in SQL
- [翻译]如何在mysql中查询每个分组的前几名
null
如何在MySQL中实现组内前几名的输出
标签:照相 color 链接 pre 元组 idt 使用 应该 技术