选择某些标记的帖子及其作者

发布于 2024-10-29 20:28:55 字数 912 浏览 2 评论 0原文

如何将这 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲琵琶半遮面シ 2024-11-05 20:28:55

您已加入 tag 表两次(因此出现错误),但尚未加入 thread 表。

SELECT thread.content, author.username
FROM tag
  JOIN thread_tags
    ON tag.id = thread_tags.tag_id
  JOIN thread                                  --join thread (not tag again)
    ON thread.id = thread_tags.thread_id
  JOIN author_threads
    ON author_threads.thread_id = thread.id     --error here too, in your query
  JOIN author
    ON author.id = author_threads.thread_id     --error here too, in your query
WHERE tag.name = 'arsenal'

You have joined the tag table twice, (thus the error) and haven't joined the thread table.

SELECT thread.content, author.username
FROM tag
  JOIN thread_tags
    ON tag.id = thread_tags.tag_id
  JOIN thread                                  --join thread (not tag again)
    ON thread.id = thread_tags.thread_id
  JOIN author_threads
    ON author_threads.thread_id = thread.id     --error here too, in your query
  JOIN author
    ON author.id = author_threads.thread_id     --error here too, in your query
WHERE tag.name = 'arsenal'
情深已缘浅 2024-11-05 20:28:55

为什么你的 JOIN 中有一个标签表?这就是您收到错误的原因:

JOIN tag ON thread_tags.tag_id = tag.id

您这里也有表标签:

FROM tag

标签表出现了两次。

why you have a tag table in your JOIN? this is why you are getting the error:

JOIN tag ON thread_tags.tag_id = tag.id

you also have table tag here:

FROM tag

tag table appeared twice.

罪#恶を代价 2024-11-05 20:28:55

您的查询中有两次标签表。也许这就是问题所在。

You have the tag table twice in your query. Maybe that's the problem.

殊姿 2024-11-05 20:28:55
SELECT thread.content, author.username
FROM thread
LEFT JOIN thread_tags ON thread.id = thread_tags.thread_id
LEFT JOIN tag ON thread_tags.tag_id = tag.id
LEFT JOIN author_threads ON author.id = author_threads.author_id
LEFT JOIN author ON author_threads.thread_id = thread.id
WHERE tag.name = 'arsenal'

顺便说一句 - 将 author_id`` 存储在thread` 表中不是更好吗?

SELECT thread.content, author.username
FROM thread
LEFT JOIN thread_tags ON thread.id = thread_tags.thread_id
LEFT JOIN tag ON thread_tags.tag_id = tag.id
LEFT JOIN author_threads ON author.id = author_threads.author_id
LEFT JOIN author ON author_threads.thread_id = thread.id
WHERE tag.name = 'arsenal'

BTW - isn't it better to store author_id`` inthread` table ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文