你怎么能加入这些5桌一起:选择某些标签的帖子及其作者
tag: id, name
author: username, id
thread_tags: thread_id, tag_id
thread: id, content
author_threads: author_id, thread_id
(我也有一个表称为author_tags(TAG_ID,AUTHOR_ID),但我不认为这里需要的多数民众赞成)。
我想选择标记某个标记及其作者的所有线程。
下面的代码返回#1066 - Not unique table/alias: 'tag'
SELECT thread.content, author.username
FROM tag
JOIN thread_tags ON thread.id = thread_tags.thread_id
JOIN tag ON thread_tags.tag_id = tag.id
JOIN author_threads ON author.id = author_threads.author_id
JOIN author ON author_threads.thread_id = thread.id
WHERE tag.name = 'arsenal'
编辑:
这工作:
SELECT thread.content
FROM tag
JOIN thread_tags ON tag.id = thread_tags.tag_id
JOIN thread ON thread.id = thread_tags.thread_id
WHERE tag.name = 'tagged'
LIMIT 0 , 30
然而,每当我试图加入的作者与他们的线程,它抛出#1066错误。
同意其中一个'标签'应该是'线程',但我认为它必须是第一个。 (你可以看到'thread'在第一个连接条件中被引用的事实。) – 2011-04-03 12:12:48
@Andriy:是的,如果我们想做最小的改变(并且查询工作!),我们必须改变第一个'tag' - >'thread'并且交换最后两个'ON ...'的条件。 – 2011-04-03 16:53:43