选择某些标记的帖子及其作者
如何将这 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错误。
How can you join these 5 tables together:
tag: id, name
author: username, id
thread_tags: thread_id, tag_id
thread: id, content
author_threads: author_id, thread_id
(I also have a table called author_tags (tag_id, author_id), but I dont think thats needed here).
I want to select all the threads which are tagged a certain tag and their authors.
The following code returns #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'
EDIT:
This works:
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
However whenever I try to join authors with their threads, it throws #1066 errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您已加入
tag
表两次(因此出现错误),但尚未加入thread
表。You have joined the
tag
table twice, (thus the error) and haven't joined thethread
table.为什么你的 JOIN 中有一个标签表?这就是您收到错误的原因:
您这里也有表标签:
标签表出现了两次。
why you have a tag table in your JOIN? this is why you are getting the error:
you also have table tag here:
tag table appeared twice.
您的查询中有两次标签表。也许这就是问题所在。
You have the tag table twice in your query. Maybe that's the problem.
顺便说一句 - 将
author_id`` 存储在
thread` 表中不是更好吗?BTW - isn't it better to store
author_id`` in
thread` table ?