Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need Select top 10 people SQL

Tags:

sql

mysql

I need SQL query for MySQL to select top 10 people with most followers

my table

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

From example user with id 6 have 3 time followed , 7->2 and 3->1, so TOP 10 will be

user with id 6,7,3 ...

like image 825
Viktors Avatar asked Dec 01 '22 21:12

Viktors


1 Answers

SELECT `follow_id`, COUNT(1) AS `followers`
FROM  `tbl`
GROUP BY `follow_id`
ORDER BY COUNT(1) DESC
LIMIT 10;
like image 102
hjpotter92 Avatar answered Dec 31 '22 03:12

hjpotter92