gpt4 book ai didi

mysql - 在 mysql 中一起使用 order by 和 group by

转载 作者:行者123 更新时间:2023-11-29 06:13:48 26 4
gpt4 key购买 nike

有一个名为 sample 的表,其中包含列名称和游戏。

name      games  
a tennis
b tennis
c football
d shuttle
e basketball
f football
g football

等等。

我需要前 2 个玩得最多的游戏和前 2 个玩得最少的游戏的列表。group by 和 order by 如何一起使用?

最佳答案

如果你想要一个计数,你可以这样做

select count(*)  as num, games from sample 
group by games
order by num

前两个

select count(*)   as num, games from sample 
group by games
order by num limit 2

最后两个

select count(*)  as num, games from sample 
group by games
order by num DESC limit 2

关于mysql - 在 mysql 中一起使用 order by 和 group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36997404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com