一次连接多个表:
我正在使用以下 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。
I'm using the following SQL statement:
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'
I have the follwing tables:
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
I keep getting the following error:
#1066 - Not unique table/alias: 'reply'
Oh and I'm using MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将表包含在
FROM
子句中,然后也加入它们 - 我认为您只需要FROM thread
,然后进行联接。You are including tables in the
FROM
clause and then joining to them as well - I think you want simplyFROM thread
, and then your joins.您在该行上收到一个错误,表明
根据给定的定义,您的author_reply 表中没有thread_id。
You got an error on the row that says
there is no thread_id in your author_reply table according to the definitions given.
这有效:
This works:
您在原始查询中的
thread
、author
和reply
之间有隐式的CROSS JOIN
,并联接了相同的表第二次没有别名。使用这个:
You had an implicit
CROSS JOIN
betweenthread
,author
andreply
in your original query, and joined the same tables for the second time without aliasing them.Use this: