tags:

views:

26

answers:

1

its been sometime since i used corelated subqueries, i am not sure if i am doing this right. in my subquery 2nd last line, i am trying to get node.id from the outer table. when i try executing the query, i get

Error Code: 1054 Unknown column 'node.id' in 'where clause')

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  A: 

It seems you just need to move your expression from clause 'from' to field list

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;

Or use single-value table like:

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;
moving the subquery to the field list works. isnt your single value table same as mine?
jiewmeng
Exactly same! I didn't catch your idea, initially
any idea why i need to move the subquery to the field list? i cannot use from subqueries
jiewmeng
So second query does not work for you? What is error?