假设我在sql server
中有两个具有父子关系的表
,如下所示,
父表:
Parentid value
1 demo
2 demo2
子表:
childid parchildid subvalue
1 1 demo1
2 1 demo2
此处子表
中的parchildid
是引用父表
.parentid
的外键
/
我需要检索特定parentid 的子表数据。所以,我使用了下面的查询
select *from child
where parchildid in (select parchildid from parent)
它给出了以下输出。 (子表
的所有行)
childid parchildid subvalue
1 1 demo1
2 1 demo2
但如您所见,我在子查询中给出了一个 invalid
列 (parchildid
)(parchildid
属于 子表
而不是父表
)。
我想知道为什么 sql server
没有抛出任何错误。
正在运行从父级中选择 parchildid
单独查询会出现无效
列错误。
谁能解释一下为什么子查询中没有抛出错误?那里的逻辑是如何运作的?
谢谢
最佳答案
来自MSDN :
If a column does not exist in the table referenced in the FROM clause of a subquery, it is implicitly qualified by the table referenced in the FROM clause of the outer query.
在您的情况下,由于 parchildid
是外部查询中表中的一列,因此没有错误。然而,查询本身无法找到这样的列,因此失败。
关于sql - Sql-Server 中的父子关系问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37339768/