我有一个类似的表结构,因为这个问题接受了答案:Recommended SQL database design for tags or tagging
除了我的
thread, thread_tags and tags
使用以下SQL命令,我可以获得与每个线程关联的一个标记,如何获得与每个线程关联的所有标记?
SELECT
thread.id AS t_id,
thread.title,
thread.content,
author.username,
author.id AS a_id
FROM thread
INNER JOIN author
ON author_id = author.id
INNER JOIN thread_tags
ON thread_id = thread.id
INNER JOIN tags
ON tag_id = tags.id
ORDER BY thread.created DESC
答案 0 :(得分:0)
我认为您的联接看起来很好,但除非您将其放在那里,否则您不会从tags
表中选择任何内容! T.*
以下
SELECT TH.id, TH.title, TH.content, A.username, A.id, T.*
FROM thread TH
INNER JOIN author A ON TH.author_id = A.id
INNER JOIN thread_tags TT ON TT.thread_id = TH.id
INNER JOIN tags T ON TT.tag_id = T.id
ORDER BY TH.created DESC