Group by,在MySQL中有使用
我的 mysql 查询有问题。 我需要获得具有最大值评级的完整行。 例如,我有表食谱
评级|编号 |类
4.1 | 1 | soup
4.2 | 3 | soup
4.9 | 4 | soup
4.8 | 5 | salads
4.3 | 6 | dishes9
SELECT MAX(rating) rat, id, class FROM recipes GROUP BY class
我得到结果:
rat |编号 | class
4.9 | 1 | soup
4.8 | 5 | salads
4.3 | 6 | dishes
因此,在第一行(其中 class='soup')我得到了真正的最大评分 - 4.9,但在这一行中 id 的值为 1,而不是 4。
当我使用 having
SELECT MAX(rating) rat, id, class FROM recipes GROUP BY class HAVING rating=rat
结果
时老鼠 |编号 | 任何人
4.8 | 5 | salads
4.3 | 6 | dishes
都可以帮我解决这个问题吗?
I have problem with mysql query.
I need get full rows with max value rating.
For example I have table recipes
rating | id | class
4.1 | 1 | soup
4.2 | 3 | soup
4.9 | 4 | soup
4.8 | 5 | salads
4.3 | 6 | dishes9
SELECT MAX(rating) rat, id, class FROM recipes GROUP BY class
And I get result:
rat | id | class
4.9 | 1 | soup
4.8 | 5 | salads
4.3 | 6 | dishes
So in first row(where class='soup') I get trully max rating - 4.9, but in this row id has value 1, not 4.
When I use having
SELECT MAX(rating) rat, id, class FROM recipes GROUP BY class HAVING rating=rat
result become
rat | id | class
4.8 | 5 | salads
4.3 | 6 | dishes
Can anyone help me with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个查询 -
Try this query -
一种方法是使用
IN()
子查询查找具有MAX(rat)
的行的对应id
。One method is to use an
IN()
subquery to find the correspondingid
s of the rows withMAX(rat)
.