TuK
TuK

Reputation: 3556

Help with count();

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

Answers (3)

Michael Berkowski
Michael Berkowski

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

Hunter McMillen
Hunter McMillen

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

Tudor Constantin
Tudor Constantin

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

Related Questions