2010-07-28 221 views
0

自從我使用核心子查詢以來,它已經有一段時間了,我不確定我是否正在做這件事。在我的子查詢的最後一行中,我試圖從外表獲得node.id。當我嘗試執行查詢,我得到MySQL相關子查詢:子查詢無法從外部查詢中查找表?

錯誤代碼:1054未知列 「node.id」在「where子句」)

select node.id, node.title, depthLookup.depth 
from posts node, (
    select count(parent.title) as depth 
    from posts parent, posts children 
    where children.lft > parent.lft 
    and children.rgt < parent.rgt 
    and children.id = node.id 
    order by parent.lft 
) as depthLookup; 

回答

2

看來你只需要移動你表達從「從」子句字段列表

select node.id, node.title, 
(
    select count(parent.title) as depth 
    from posts parent, posts children 
    where children.lft > parent.lft 
    and children.rgt < parent.rgt 
    and children.id = node.id 
    order by parent.lft 
) as depthLookup 
from posts node; 

或者使用單值表所示:

select node.id, node.title, depthLookup.depth 
from posts node, 
(
    select count(parent.title) as depth 
    from posts parent, posts children 
    where children.lft > parent.lft 
    and children.rgt < parent.rgt 
    and children.id = node.id 
    order by parent.lft 
) as depthLookup; 
+0

將子查詢移動到字段列表中。是不是你的單一值表與我的相同? – 2010-07-28 06:51:10

+0

完全一樣!我沒有發現你的想法,最初 – burnall 2010-07-28 07:31:25

+0

任何想法,爲什麼我需要將子查詢移動到字段列表?我無法使用子查詢 – 2010-07-29 08:25:14