1

我有 3 张桌子。

  Video (Id, ViewCount, Location)
  Likes (Id, Video, User)
  Location (Id, Name) 

我将如何查询排名最高的四个视频(Video.ViewCount + count(Likes.User) 的总和按 Video.Id 对它们进行分组,并从特定位置返回前 4 个结果。

我试过这个:

SELECT  Video.Id, sum(Video.ViewCount + count(Likes.User)) as Points From Video
  Left Join Likes
  ON Likes.Video=Video.Id
  WHERE Video.Location=30
  GROUP BY Video.Id
  ORDER BY Points DESC
  LIMIT 4;

但是我对组功能的使用无效。有没有人有任何指针?

4

1 回答 1

3
SELECT  id,
        viewcount +
        (
        SELECT  COUNT(*)
        FROM    likes l
        WHERE   l.video = v.id
        ) AS points
FROM    video v
WHERE   location = 30
ORDER BY
        points DESC, id
LIMIT 4
于 2011-11-02T16:59:59.313 回答