我正在尝试输出表,其中每一行代表线程中的帖子,而列将线程细分为问题、答案和评论。我有问答部分,现在我需要添加评论部分。
我试图了解我给出的解决方案,以便使用代码并为评论者添加第三列。粘贴的代码从一列学生 ID 创建两列。第一列是所有发布问题的学生,第二列是在给定线程中发布答案的所有学生。
表查询自:
thread_id student_usrnm post_type
1 iron_man question
1 orient answer
1 cyclops comment
2 green_lantern question
2 iron_man answer
... .... .....
输出:
questioners answerers commenters
iron_man orient cyclops
green_lantern iron_man
这是有效并生成提问者和回答者的代码:
SELECT s1.author_id AS questioner,
(SELECT group_concat(DISTINCT author_id SEPARATOR " ") FROM students s2 WHERE s2.post_type = 'answer' AND s2.thread_id = s1.thread_id) AS answerers
FROM students s1
WHERE s1.post_type = 'question';
问题:
什么是 s1,什么是 s2,它们是如何工作的?他们是临时表还是什么?
如何为评论员添加第三列?这是我生成第三列的可悲尝试:
SELECT s1.author_id AS questioner, (SELECT group_concat(DISTINCT author_id SEPARATOR " ") FROM students s2 WHERE s2.post_type = 'answer' AND s2.thread_id = s1.thread_id) AS answerers, (SELECT group_concat(DISTINCT author_id SEPARATOR " ") FROM students s3 WHERE s3.post_type = 'comment' and s3.thread_id = s1.thread_id) AS commenters, FROM students s1 WHERE s1.post_type = 'question';