LeetCode(数据库)- 每个帖子的评论数
Posted 程序员牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 每个帖子的评论数相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:略。
AC 代码
-- 解决方案(1)
SELECT t1.sub_id post_id, COUNT(t2.parent_id) number_of_comments
FROM (SELECT * FROM Submissions WHERE parent_id IS NULL GROUP BY sub_id) t1
LEFT JOIN
(SELECT * FROM Submissions WHERE parent_id IS NOT NULL GROUP BY sub_id, parent_id) t2
ON t1.sub_id = t2.parent_id
GROUP BY post_id
ORDER BY post_id
-- 解决方案(2)
SELECT post_id, COUNT(sub_id) AS number_of_comments
FROM (
SELECT DISTINCT post.sub_id AS post_id, sub.sub_id AS sub_id
FROM Submissions post
LEFT JOIN Submissions sub
ON post.sub_id = sub.parent_id
WHERE post.parent_id is null
) T
GROUP BY post_id
ORDER BY post_id ASC
以上是关于LeetCode(数据库)- 每个帖子的评论数的主要内容,如果未能解决你的问题,请参考以下文章