2013-07-27 90 views
1

我有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不起作用。

回答

0

您不需要在JOIN之前使用AND來加入更多的兩個表。

您的查詢應該是

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

+1

我尋找答案了幾個小時,只是有點**「和」 **感謝您的幫助。 :) –