0

自从我使用相关子查询以来已经有一段时间了,我不确定我是否做对了。在我的子查询第二行最后一行中,我试图node.id从外部表中获取。当我尝试执行查询时,我得到

错误代码:1054 'where 子句'中的未知列'node.id')

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;
4

1 回答 1

2

看来您只需要将表达式从子句 'from' 移动到字段列表

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;
于 2010-07-28T05:40:02.703 回答