我有 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;
但是我对组函数的使用无效。有人有任何指示吗?
最佳答案
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
关于MySQL,混淆查询/错误代码 : 1111. 组函数的使用无效。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7984264/