1

我有一个表格,用于维护与视频及其喜好相关的信息。

在此处输入图像描述

所以我想获得最喜欢的videoid,我通过以下方式查询

选择 VideoId,count(LikeStatus) from Likes where Status='0' group by VideoId

我得到以下输出

在此处输入图像描述

工作正常,但我需要这个查询是嵌套查询我的意思

select title,desc from videos where videoID IN (select VideoId,count(LikeStatus) from Likes where Status='0' group by VideoId)

我知道子查询必须只返回一个字段,那么还有其他好的方法吗?

4

2 回答 2

3

像这样的东西?

SELECT *
FROM   Videos
 INNER
  JOIN (
        SELECT VideoId
             , Count(*) As number_of_likes
        FROM   Likes
        WHERE  Status = '0'
        GROUP
            BY VideoId
       ) As likes
    ON likes.VideoId = Videos.VideoId
于 2013-11-05T12:55:30.790 回答
2

如果我理解得很好,您尝试执行以下代码(?)

SELECT title,desc 
FROM videos V
LEFT JOIN Likes L ON V.id = L.VideoId

WHERE (select count(LikeStatus) from Likes DL where DL.Status='0' AND DL.VideoId = V.id group by DL.VideoId) > 0

GROUP BY V.id

ORDER BY (select count(LikeStatus) from Likes DL where DL.Status='0' AND DL.VideoId = V.id group by DL.VideoId) DESC

我对吗?

于 2013-11-05T13:16:32.777 回答