简体   繁体   中英

Can not join more than two table

I have 3 tables:

table1

fields : id, title, cat1_id

table2

fields : id, title, cat2_id

table3 

fields : id, title

My SQL :

SELECT a.id, a.title, b.title, c.title 
FROM table1 AS a 
INNER JOIN table2 AS b ON b.id = a.cat1_id 
AND INNER JOIN table3 AS c ON c.id = b.cat2_id 
ORDER BY a.id DESC

it's not working.

I try this SQL, it's working:

SELECT a.id, a.title, b.title, c.title 
FROM table1 AS a 
INNER JOIN table2 AS b ON b.id = a.cat1_id 
ORDER BY a.id DESC

but double INNER JOIN does not work.

You do not need to use AND before JOIN to join more that two tables.

Your query should be

SELECT a.id, a.title, b.title, c.title 
FROM table1 AS a 
INNER JOIN table2 AS b ON b.id = a.cat1_id 
INNER JOIN table3 AS c ON c.id = b.cat2_id 
ORDER BY a.id DESC

Have a look at MySQL JOIN Syntax

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