MySQL 相关子查询:子查询无法从外部查询中找到表?
自从我使用相关子查询以来已经有一段时间了,我不确定我这样做是否正确。在我的子查询第二行中,我试图从外表获取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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您只需将表达式从子句“from”移至字段列表
或使用单值表,例如:
It seems you just need to move your expression from clause 'from' to field list
Or use single-value table like: