时间:2021-07-01 10:21:17 帮助过:19人阅读
有如上的表,我们需要找出每个类型(type)中最便宜的前两种水果,我们可以采取这样的方法:
select type, variety, pricefrom fruitswhere (select count(*) from fruits as fwhere f.type = fruits.type and f.price < fruits.price) <= 1;
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;
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
如何在MySQL中实现组内前几名的输出
标签:照相 color 链接 pre 元组 idt 使用 应该 技术