我的数据库中有两个表。
我需要使用前5个评论新闻(有效评论和活动新闻),并使用一个查询列出他们的标题。我希望我有道理。但我真的很困惑。任何建议都表示赞赏。
答案 0 :(得分:1)
这是你想要的吗?
SELECT TOP(5) News.*
FROM News
WHERE News.NewsActive = 1
ORDER BY
(SELECT COUNT(*)
FROM Comments
WHERE Comments.NewsId = News.NewsId AND Comments.CommentsActive = 1) DESC;
对您的评论的回复类似于:
SELECT TOP(5) News.*,
(SELECT COUNT(*)
FROM Comments
WHERE Comments.NewsId = News.NewsId AND Comments.CommentsActive = 1) AS TotalComments
FROM News
WHERE News.NewsActive = 1
ORDER BY TotalComments DESC;