本文介绍了在SQL中,如何获取具有特定列最大值的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询:
SELECT COUNT(*) as votes, a.member_id
FROM ballots a
WHERE ballot_id = 1
GROUP BY a.member_id
其结果类似于:
votes member_id
1 paul
5 mike
3 noynoy
10 andy
2 noel
我希望能够获得安迪"这一行,因为他获得了最高的票数".
I'd like to be able to get the row "andy" because he got the highest "votes".
如何更改查询以执行此操作?
How do I change my query to do this?
在此先感谢您的帮助:)
Thanks in advance for your help :)
推荐答案
您可以使用ORDER BY
然后按DESC
降序从最高到最低投票对其进行排序.然后LIMIT
结果仅显示一行(即最高的一行).
You can order it from highest to lowest votes using ORDER BY
and then DESC
for descending order. Then LIMIT
the results to only one row (i.e. the highest).
SELECT COUNT(*) as votes, a.member_id
FROM ballots a
WHERE ballot_id = 1
GROUP BY a.member_id
ORDER BY votes DESC
LIMIT 1
这篇关于在SQL中,如何获取具有特定列最大值的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!