I have a forum. I want to perform an SQL query so I can get the user who has the most blog submissions. I have 2 tables, blogs and users.
The blogs table has author_id which is the user_id from the users table.
So here is what I tried to do but without success:
SELECT b.author_id FROM blogs b
INNER JOIN users u ON b.author_id = u.user_id
ORDER BY count(author_id) DESC
GROUP BY b.author_id
LIMIT 0,10;
Could you please help me to find the user with most blog submissions, or in other words, I need to find a author_id that has most occurrence in the blogs table. Thanks!
You don’t need to join to
usersfor this, you just need a GROUP BY and a LIMIT:The GROUP BY tells the COUNT which groups it should count; in this case, it should collect up the blogs for each
author_idand count those as a single group.