I have this SQL query:
SELECT count(commentratings.ID) AS count, user_ID FROM `commentratings`
WHERE comment_ID = ANY (SELECT ID FROM `ratings` WHERE user_ID = 2) AND rating_direction = 1
It returns the number of upvotes for the user with user_ID = 2 (as expected).
I would like to get this count not only for this particular user, but for all users in the database. I get all relevant IDs with
SELECT user_ID from users
How can I alter the first query so that it returns all counts for all users? It would have to be some kind of for loop, I suppose, which loops over the ID in the subquery. Pseudocode:
for i in (SELECT user_ID from users):
SELECT count(commentratings.ID) AS count, user_ID FROM `commentratings`
WHERE comment_ID = ANY (SELECT ID FROM `ratings` WHERE user_ID = i) AND
rating_direction = 1
How do I implement this with SQL? Thanks a lot!