【发布时间】:2011-04-12 17:23:47
【问题描述】:
我在 mySQL 数据库中有下表,(注意:这些是总结的,因此它们与这个问题相关,一些列被省略了)。
author (id, username, password etc.)
thread (id, title, content)
tag (id, name)
reply (id, content)
thread_replies (thread_id, reply_id)
author_replies (author_id, reply_id)
thread_tags (thread_id, tag_id)
author_threads (author_id, thread_id)
现在要获取某个作者的主题,我通常会这样做:
SELECT thread.title, thread.id AS thread_id, thread.content, author.username, author.id AS author_id
FROM thread
JOIN author_threads ON thread.id = author_threads.thread_id
JOIN author ON author_threads.author_id = author.id
WHERE author.id = '12'
这很好用,但是当我尝试获取与这些线程关联的标签时:
SELECT thread.title, thread.id AS thread_id, thread.content, author.username, author.id AS author_id, GROUP_CONCAT( DISTINCT tag.name
ORDER BY tag.name DESC
SEPARATOR ',' ) AS tags
FROM thread
JOIN thread_tags ON thread.id = thread_tags.thread_id
JOIN tag ON thread_tags.tag_id = tag.id
JOIN author_threads ON thread.id = author_threads.thread_id
JOIN author ON author_threads.author_id = author.id
WHERE author.id = '12'
LIMIT 0 , 30
它只在一个列中显示第一个线程,以及与该作者关联的所有标签。
我在这里做错了什么?
另外,如果我想计算作者对每个线程的回复数,该怎么做?
【问题讨论】:
-
主题是否有多个作者?
-
不!我没有设计它,我被告知他们这样做是因为他们想使用索引(或类似的东西)。