2012-10-19 63 views
0

我想從我的數據庫中的菜單項列表中的多層菜單放在一起。這是我的查詢來做到這一點。 FL =第一級,SL =第二級等使用嵌套連接的SQL查詢並選擇

這裏的查詢:

SELECT name as flName,id AS flId 
FROM menu_items AS m1 
WHERE parent_id = 0 
INNER JOIN (SELECT name AS slName, id AS slId, parent_id AS slPid FROM menu_items) 
m2 ON m1.firstLevelId = m2.parent_id 
INNER JOIN (SELECT name AS tlName, id AS tlId, parent_id AS tlPid FROM menu_items) 
m3 ON m2.id = m3.parent_id 

而我得到的錯誤是這樣的:

PersistenceException: Query threw SQLException: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 'INNER JOIN (SELECT name AS slName, id AS slId, parent_id AS slPid FROM 
menu_item' 

但它看起來有效的給我,我不知道。很確定我以前在子查詢上做過連接。我在做別的事嗎?

回答

3

where子句移動到結束,像這樣:

SELECT name as flName,id AS flId 
FROM menu_items AS m1 INNER JOIN (
     SELECT name AS slName, id AS slId, parent_id AS slPid FROM menu_items 
    ) m2 ON m1.firstLevelId = m2.parent_id 
    INNER JOIN (
     SELECT name AS tlName, id AS tlId, parent_id AS tlPid FROM menu_items 
    ) m3 ON m2.id = m3.parent_id 
WHERE parent_id = 0 
+0

是的,有時是小事情... –

+0

要去現在提交的SQL誘發自殺...... –