Reputation: 5547
I'm trying to get the total sum of likes a user has on his posts in SQL. Essentially I have the following table design:
Table Content :
Table Likes :
So essentially what I need to do is get all of the content_id
from the Content
table, where poster_id == user
and then for each of those content_id
I need to get all likes from the Likes
table. It's easy to do these queries separately, but I'm not sure how to combine it into one query.
Upvotes: 0
Views: 17
Reputation: 780688
Simply join the two tables and use COUNT(*)
to count the selected rows.
SELECT COUNT(*) AS likes
FROM Likes AS l
JOIN Content AS c ON l.content_id = c.content_id
WHERE c.poster_id = user
AND l.upvoteOrDownVote = 'upvote'
Upvotes: 1