2011-01-06 51 views
1

所有標籤我有一個類似的表結構,是這個問題,接受的答案:Recommended SQL database design for tags or tagging獲取在PHP

除礦

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

Post:http://stackoverflow.com/questions/4612353/inner-joins-in-php ??? ??? – Chandu 2011-01-06 07:18:53

+0

什麼是`author_id`?你應該把表名放在任何地方,或者更好的是縮短名字(例如`TH從線程TH INNER JOIN作者A上TH.author_id = A.id` – 2011-01-06 07:23:53

回答

0

我覺得你的加入看起來很好,但你的arent要選擇從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