需要选择前10名SQL人员

时间:2012-07-20 11:56:47

标签: mysql sql

我需要MySQL的SQL查询来选择拥有最多粉丝的前10名

我的表

id | user_id | follow_id
1     3          6
2     3          7
3     4          6
4     5          6
5     7          3
6     9          7

来自id为6的示例用户有3个时间,7-> 2和3-> 1,所以TOP 10将是

ID为6,7,3 ...

的用户

7 个答案:

答案 0 :(得分:8)

SELECT `follow_id`, COUNT(1) AS `followers`
FROM  `tbl`
GROUP BY `follow_id`
ORDER BY COUNT(1) DESC
LIMIT 10;

答案 1 :(得分:2)

SELECT follow_id,count(id) AS cnt FROM table 
GROUP BY follow_id ORDER BY cnt DESC  LIMIT 10

答案 2 :(得分:2)

您可以使用

SELECT user_id , COUNT(id) AS count FROM tbl GROUP BY follow_id ORDER BY count DESC LIMIT 10;

答案 3 :(得分:1)

您想使用MySQL GROUP BY聚合函数

SELECT user_id, COUNT(follow_id) AS total_followers
FROM users 
GROUP BY follow_id 
ORDER BY total_followers LIMIT 10;

答案 4 :(得分:1)

尝试这样的事情:

select follow_id
from myTable
group by follow_id
order by count(user_id)
Limit 10

答案 5 :(得分:1)

您需要通过follow_id对结果进行分组,然后计算此组中的结果数量,并按降序排列每个组的结果数,然后定义您希望将其限制为仅10个结果使用LIMIT 0,10

完成

以下查询在MySQL 5中完美运行

SELECT follow_id, COUNT(follow_id) AS nr
FROM test.testtable
GROUP BY follow_id
ORDER BY nr DESC
LIMIT 0,10 

答案 6 :(得分:0)

SELECT    follow_id,
          COUNT(user_id) AS number_of_followers
    FROM  table
    GROUP BY follow_id
    ORDER BY number_of_followers DESC
    LIMIT 10;