内部连接语句的mysql语法错误

时间:2011-11-12 11:18:12

标签: php mysql

我想从两个表中获取最后5行。

这是我执行的mysql语句:

"SELECT title, thread_id
FROM threads AS t1
JOIN ON comments AS c1 ON t1.user_id = c1.user_id
 WHERE t1.user_id=".$userID
." OR c1.user_id=".$userID.
" ORDER BY thread_id DESC
LIMIT 0,5"

这是错误:

  

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在正确的语法附近使用' ON注释AS c1 ON t1.user_id = c1.user_id WHERE t1.user_id = 1 OR c1。'在第3行

我做错了什么?!?

3 个答案:

答案 0 :(得分:2)

ON之前你有一个多余的comments

答案 1 :(得分:2)

JOIN ON之类的内容不应只是JOININNER JOINLEFT JOIN。像这样的东西:

"SELECT title, thread_id
FROM threads AS t1
JOIN comments AS c1 ON t1.user_id = c1.user_id
 WHERE t1.user_id=".$userID
." OR c1.user_id=".$userID.
" ORDER BY thread_id DESC
LIMIT 0,5"

答案 2 :(得分:0)

删除第3行中的额外ON。它会起作用。

"SELECT title, thread_id
FROM threads AS t1
JOIN comments AS c1 ON t1.user_id = c1.user_id
 WHERE t1.user_id=".$userID
." OR c1.user_id=".$userID.
" ORDER BY thread_id DESC
LIMIT 0,5"