So I have a “bookmark” table for which i am trying to get the “UserID”s of the users that have bookmark the most content. This query returns the UserID of the ten most active users. This query looks like this for me and works:
SELECT COUNT(*) AS `Rows`, UserID FROM `bookmark`
WHERE UserID NOT IN(1, 2, 25, 38, 41, 43, 47, 125)
GROUP BY UserID ORDER BY `Rows` DESC LIMIT 10
Now i am trying to join the result queries to the “accounts” table to get all the info of each user ID by comparing the UserID to the “id” in the accounts table.
SELECT COUNT(*) AS `Rows`, UserID FROM `bookmark`
join accounts ON accounts.ID = bookmark.UserID
WHERE UserID NOT IN(1, 2, 25, 38, 41, 43, 47, 125)
GROUP BY UserID ORDER BY `Rows` DESC LIMIT 10
This is not returning any of the rows as the accounts table but is instead returning the same result as before. I need to be able to get all the rows of the accounts table (same thing as using *)
If it helps my accounts table has these rows = user_name, id, email and my bookmark table has these rows = id, UserId, link
One solution is to move the
group byto a subquery:Another is to add the columns from accounts to the group by:
Note: MySQL allows you to list only
bookmark.UserIDin thegroup by; although that will work, it’s not recommended.