我的查询试图解决表格中的所有问题。
问题 帖子 话题 主题映射
我的标签表设置有第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
非常感谢
答案 0 :(得分:5)
您需要先将问题加入映射表。
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