我有一个小问题。我在数据库中有一个表,例如:
id | AuthorId | Book
---------------------
1 | 2 | book name
2 | 2 | book name
3 | 2 | book name
4 | 2 | book name
5 | 5 | book name
6 | 5 | book name
7 | 8 | book name
8 | 9 | book name
9 | 9 | book name
10 | 6 | book name
如您所见,在此表中,作者ID“ 2”被重复最高的次数(4次),而作者的Author ID出现的次数少于4次。如何使用php MySQLi从此表中获取ID“ 2”,这是此数据中最经常出现的值?我不知道该怎么办。
非常感谢你。
请尝试以下查询。您可以对按AuthorID DESC分组的行进行计数,并将结果限制为最大值。
SELECT AuthorId, COUNT(AuthorId) as c from table
GROUP BY AuthorId
ORDER BY COUNT(AuthorId) DESC
LIMIT 1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句