我有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;
但我无效使用群组功能。有没有人有指针?
答案 0 :(得分: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