选择列值的最小出现次数

时间:2016-08-12 22:21:45

标签: mysql

我很难从下表中获得以下内容:

原件:

+--------+------------+-------+
| type   | variety    | price |
+--------+------------+-------+
| apple  | gala       |  2.79 | 
| apple  | gala       |  2.99 | 
| apple  | gala       |  3.45 | 
| apple  | fuji       |  0.24 | 
| apple  | limbertwig |  2.87 | 
| orange | valencia   |  3.59 | 
| orange | navel      |  9.36 | 
| pear   | bradford   |  6.05 | 
| pear   | bartlett   |  2.14 | 
| cherry | bing       |  2.55 | 
| cherry | chelan     |  6.33 | 
+--------+------------+-------+

我真正想要的是什么:

+--------+------------+-------+
| type   | variety    | price |
+--------+------------+-------+
| apple  | gala       |  2.79 | 
| apple  | fuji       |  0.24 | 
| apple  | limbertwig |  2.87 | 
+--------+------------+-------+

所以我基本上希望能够选择所有苹果类型,但每种价格中最低的。

如何在使用mysql查询时实现这一目标?

1 个答案:

答案 0 :(得分:1)

SELECT type, variety, MIN(price) AS price
FROM yourTable
WHERE type = 'apple'
GROUP BY type, variety