Reputation: 3556
I've got 2 tables. One called Video, and one called ThumbsUp.
Video -> Id, Path, Thumbnail, Profile
ThumbsUp -> Id, Owner, Video
I need to pull every Video record WHERE PROFILE = 2
,
And COUNT(ThumbsUp.Id)
the number of likes for each video,
Even if that video does not have any thumbs up.
Any help is appreciated. . .
Upvotes: 0
Views: 67
Reputation: 270637
SELECT Video.Id, COUNT(ThumbsUp.Video)
FROM
Video LEFT JOIN ThumbsUp ON Video.Id = ThumbsUp.Video
WHERE Video.Profile = 2
GROUP BY Video.Id
Upvotes: 3
Reputation: 61510
SELECT V.*, COUNT(T.id) FROM Video V
OUTER JOIN ThumbsUp T ON T.Video = V.id
WHERE V.profile == 2;
Edit: added outer join
Upvotes: -2
Reputation: 26861
SELECT v.*, count(tu.id)
FROM video v
LEFT JOIN ThumbsUp tu ON tu.video_id = v.id
WHERE v.profile = 2
GROUP BY v.id
Upvotes: 1