MySQL,困惑w /查询/错误代码:1111。无效使用组功能。

时间:2011-11-02 16:56:43

标签: mysql sql mysql-error-1111

我有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;

但我无效使用群组功能。有没有人有指针?

1 个答案:

答案 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