有一个名为 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/