帮助count();

时间:2011-07-22 18:03:01

标签: mysql sql database

我有两张桌子。 一个叫做Video,一个叫做ThumbsUp。

Video    -> Id, Path, Thumbnail, Profile

ThumbsUp -> Id, Owner, Video

我需要提取每条视频记录WHERE PROFILE = 2COUNT(ThumbsUp.Id)每个视频的喜欢数量, 即使那段视频没有竖起大拇指。

感谢任何帮助。 。

3 个答案:

答案 0 :(得分:3)

SELECT Video.Id, COUNT(ThumbsUp.Video) 
FROM 
  Video LEFT JOIN ThumbsUp ON Video.Id = ThumbsUp.Video
WHERE Video.Profile = 2
GROUP BY Video.Id

答案 1 :(得分:1)

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 

答案 2 :(得分:-2)

SELECT V.*, COUNT(T.id) FROM Video V
OUTER JOIN ThumbsUp T ON T.Video = V.id
WHERE V.profile == 2;

编辑:添加外部联接