I have the table users with information about every user. Then the table posts with information about articles and finally the table user_posts, which contains following columns:
user_id
post_id
...
I am trying to get the chart of users with the highest count of posts. I made this query:
SELECT u.id as uid, u.name as uname,
count(up.id) as up_count
FROM users as u JOIN user_posts as up ON up.user_id = u.id ORDER BY vcount DESC LIMIT 25
This query returns me only one user and the total count of all rows in the table user_posts.
What am I doing wrong? I need to get the list of 25 users sorted by the count of articles that published each user.
Thank you in advance
Your query needs to have
GROUP BYclause because you have usedCOUNT()function.You must have grouped them by
IDotherwise you’ll single total count result for all records. One more thing, useLEFT JOINso even users with no post still will be visible in you result with the score of0.