不能加入两个以上的表

时间:2013-07-27 07:48:42

标签: mysql sql double inner-join

我有3张桌子:

table1

fields : id, title, cat1_id

table2

fields : id, title, cat2_id

table3 

fields : id, title

我的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

它不起作用。

我尝试这个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 
ORDER BY a.id DESC

但是双INNER JOIN不起作用。

1 个答案:

答案 0 :(得分:0)

您不需要在AND之前使用JOIN来加入更多这两个表格。

您的查询应该是

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

查看MySQL JOIN Syntax