我有一张表格,用于维护与视频及其相关内容相关的信息。
所以我想得到最喜欢的视频,因为我用以下方式查询
选择VideoId,计数(LikeStatus)来自Likes,其中Status ='0'分组由VideoId
我得到以下输出
工作正常,但我需要这个查询是嵌套查询我的意思是
select title,desc from videos where videoID IN (select VideoId,count(LikeStatus) from Likes where Status='0' group by VideoId)
我知道子查询必须只返回一个字段,那么还有其他好的方法吗?
答案 0 :(得分: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
答案 1 :(得分: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
我是对的吗?