我有一个查询试图解决我表中的所有问题.
问题帖子主题主题映射
我的标签表设置有第3个表,用于将问题ID与主题ID映射.
但是,如何使用JOIN语句提取主题表中存储的主题的名称
所以基本上我不知道如何在只有主题ID而不是主题名称的表上执行JOIN语句
SELECT questions.*
, posts.post
, COUNT(posts.post) as total_answers
, posts.votes
, posts.id as post_id
, posts.created
, users.id as user_id
, users.username, users.rep
, topics.name
FROM questions
LEFT JOIN posts ON questions.id = posts.question_id
LEFT JOIN users ON questions.user_id = users.id
LEFT JOIN topics ON topic_mapping.question_id = questions.id
GROUP BY questions.id
Run Code Online (Sandbox Code Playgroud)
非常感谢
您需要先将问题加入映射表.
SELECT questions.*
, posts.post
, COUNT(posts.post) as total_answers
, posts.votes
, posts.id as post_id
, posts.created
, users.id as user_id
, users.username, users.rep
, topics.name
FROM questions
LEFT JOIN posts ON questions.id = posts.question_id
LEFT JOIN users ON questions.user_id = users.id
LEFT JOIN topic_mapping ON questions.id = topic_mapping.question_id
LEFT JOIN topics ON topic_mapping.topic_id = topics.id
GROUP BY questions.id
Run Code Online (Sandbox Code Playgroud)