Is this possible to do this in one MySQL query? Basically I need to sort users by how many responses they have. I have 2 tables
table "users":
id username
------------------
1 hunter
2 loserville
and another
table "responses":
id user_id response
-------------------------------
1 1 yes
1 2 yes
1 1 no
1 1 yes
I need something like this
SELECT users.id
FROM users
UNION ALL
SELECT COUNT(responses.id) As num_responses
FROM responses
WHERE user_id = users.id
ORDER BY num_responses DESC
Unfortunately it doesn't work. Any suggestions? Let me know if you are confused! Thanks for your time.