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

标签 mysql

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

相关文章:

mysql - Rails/MySQL : Group/Distinct doubles query time using LEFT JOINS/slow performance

php - 在 php 表中显示包含行的列

mysql - sphinx 配置: Adding manual column what is not found on MySQL Database

php - 无法比较 mysql 和 php 中的时间

mysql - 具有条件和限制的 Symfony/Doctrine ManyToMany

php - 显示 MYSQL 结果时出现问题

mysql - 在 Hibernate 中处理死锁的正确方法

php - MySQL INSERT INTO 不执行任何操作 - 甚至没有错误

PHP/MySQL - 如何证明用户编辑了值而不是管理员

java - Hibernate:在不实际使用任何主/外键关系的情况下使用 OneToMany