我有三个表 A B C,我正在尝试从这三个表中检索信息。
A 具有列 userid 头像用户名,B 具有列 postid, dateshared,C 具有列评论者 postid datecommented。
我正在尝试运行查询
Select C.comment, C.commenter, C.datecommented, B.postid, B.dateshared A.username A.avatar from B Left Join C Left join A on C.postid = B.postid AND A.userid = C.commenter where B.postid IN ('1','2','3') order by C.dateshared desc
但它给出了以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where B.postid IN ('1', '2', '3') order by C.dateshared '
任何人都可以指出我做错了什么或建议如何解决吗?
最佳答案
每个 LEFT JOIN
都需要自己的 ON
条件:
SELECT C.comment, C.commenter, C.datecommented, B.postid, B.dateshared, A.username A.avatar
FROM B
LEFT JOIN
C
ON C.postid = B.postid
LEFT JOIN
A
ON A.userid = C.commenter
WHERE B.postid IN ('1','2','3')
ORDER BY
C.dateshared desc
关于使用 Left Join 和Where IN 子句的 MYSQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16071871/