简体   繁体   中英

mysql syntax error on inner join statement

I want to get the 5 last rows from two tables.

Here is the mysql statement I execute:

"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"

Here is the mistake:

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 'ON comments AS c1 ON t1.user_id = c1.user_id WHERE t1.user_id=1 OR c1.' at line 3

What am I doing wrong?!?

你有多余的ON之前comments

There isnt anything like JOIN ON should be just JOIN or INNER JOIN , LEFT JOIN . something like this:

"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"

Remove the extra ON in 3rd line. It will works.

"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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM