【问题标题】:Getting all tags in php获取php中的所有标签
【发布时间】:2011-06-04 12:03:35
【问题描述】:

我有一个与此问题相似的表结构接受答案: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

【问题讨论】:

标签: mysql tags


【解决方案1】:

我认为您的联接看起来不错,但除非您将其放在那里,否则您不会从 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 

【讨论】:

  • 我仍然只得到一个标签,我想得到与每个线程关联的所有标签。
  • 我不明白为什么,所以让我们先了解一下基础知识。你能显示一些(10-20)行thread_tags吗? thread_tags 是一个多对多的 tag.idthread.id 对吗?
  • 尝试命名每个表,失去顺序和作者。看看你是否可以正确加入线程、thread_tags 和标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2021-05-13
相关资源
最近更新 更多