获取具有最大相同值的列

时间:2014-10-16 17:26:56

标签: php mysql

我有这张桌子:

follower | following
2        | 1
3        | 1
1        | 13
13       | 1
2        | 1
24       | 18
24       | 5
5        | 24

它包含彼此跟随的不同用户的用户名。最受关注的人是1,所以我想显示一个输出,如:“本网站上最关注的人是1人,有4个关注”。

2 个答案:

答案 0 :(得分:1)

这样的东西
SELECT following, 
       COUNT(follower) AS followercount
FROM mytable
GROUP BY following
ORDER BY 2 DESC
LIMIT 1;

DEMO

答案 1 :(得分:1)

结帐GROUP BY

SELECT following, COUNT(*) AS followcount
FROM follows
GROUP BY following
ORDER BY followcount DESC
LIMIT 1;

Demo