我有这样的一张 table
编号 |名称 |用户 ID |出价|电子邮件
在一行中我有这样的东西
1 | Michael | 34 | 45 | email@email.com
2 | Michael | 34 | 45 | email@email.com
3 | John | 34 | 45 | email@email.com
4 | John | 34 | 45 | email@email.com
5 | John | 34 | 45 | email@email.com
6 | Jovan | 34 | 45 | email@email.com
7 | Peter | 34 | 45 | email@email.com
8 | Michael | 34 | 45 | email@email.com
9 | Oliver | 34 | 45 | email@email.com
10 | Tataian | 34 | 45 | email@email.com
我需要一个查询来查看我拥有最多的名字以及将它们限制在前 5 名的次数。
我试过类似的东西,但我只得到第一个,我需要五个名字和多少次。
这就是我现在拥有的
SELECT count(`Name`) as Requests, `Name` as Names WHERE 1
最佳答案
您应该能够按名称分组,按计数和限制排序:
SELECT COUNT(`Name`) AS `Requests`, `Name` AS `Names`
FROM `table`
GROUP BY `Names`
ORDER BY `Requests` DESC
LIMIT 5
基于上表的结果将是这样的:
Requests | Names
--------------------------
3 | John
3 | Michael
1 | Jovan
1 | Oliver
1 | Peter
关于php - MySQL 算作最高请求,限制为 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34773866/