2

我正在使用以下 SQL 语句:

SELECT reply.id, reply.content, author.username
FROM thread, reply, author
JOIN thread_reply ON thread.id = thread_reply.thread_id
JOIN reply ON thread_reply.reply_id = reply.id
JOIN author_reply ON thread.id = author_reply.thread_id
JOIN author ON author_reply.author_id = author.id
WHERE thread.id = '40'

我有以下表格:

thread_reply: thread_id, reply_id

reply: id, content, created (timestamp)

author: id, username, password_hash, salt #etc

thread: id, content, created

author_reply: author_id, reply_id

我不断收到以下错误:

#1066 - Not unique table/alias: 'reply'

哦,我正在使用 MySQL。

4

5 回答 5

3

您在原始查询中有一个隐含的CROSS JOINbetween thread, authorand reply,并且第二次加入了相同的表而不给它们加上别名。

用这个:

SELECT  reply.id, reply.content, author.username
FROM    thread t
JOIN    thread_reply tr
ON      tr.thread_id = t.id
JOIN    reply r
ON      r.id = tr.reply_id
JOIN    author_reply ar
ON      ar.reply_id = r.id
JOIN    author a
ON      a.id = ar.author_id
WHERE   thread.id = '40'
于 2011-04-01T14:17:26.097 回答
1
SELECT reply.id, reply.content, author.username
FROM thread
INNER JOIN thread_reply ON thread.id = thread_reply.thread_id
INNER JOIN reply ON thread_reply.reply_id = reply.id
INNER JOIN author_reply ON thread.id = author_reply.thread_id
INNER JOIN author ON author_reply.author_id = author.id
WHERE thread.id = '40'
于 2011-04-01T14:17:24.057 回答
1

您在FROM子句中包含表,然后也加入它们 - 我认为您想要简单FROM thread,然后加入。

于 2011-04-01T14:17:33.653 回答
0

这有效:

SELECT reply.id, reply.content, author.username
FROM thread
JOIN thread_reply ON thread.id = thread_reply.thread_id
JOIN reply ON thread_reply.reply_id = reply.id
JOIN author_reply ON reply.id = author_reply.reply_id
JOIN author ON author_reply.author_id = author.id
WHERE thread.id = '40'
于 2011-04-01T14:31:55.020 回答
0

您在显示的行上出现错误

JOIN author_reply ON thread.id = author_reply.thread_id

根据给定的定义,您的 author_reply 表中没有 thread_id。

于 2011-04-01T14:22:55.383 回答