繁体   English   中英

SQL 子查询将只检查一列,当要求检查同一查询时失败

[英]SQL sub-query will only check one column, fails when ask to check 2 for the same query

当我在 SQL Server 2014 Express 中运行以下代码时,可以很好地获取这个父亲的所有孩子的家庭关系表。

select Name 
from Persons 
where ID in (select personID 
             from FAMILY 
             where fatherID in (select fatherID.ID 
                                from Persons fatherID 
                                where fatherID.Name = 'Louis Vingo'))  

但是当我尝试用这段代码检查母亲和父亲时,我得到了一个错误。

select Name 
from Persons 
where ID in (select personID 
             from FAMILY 
             where (fatherID, motherID) in (select fatherID.ID, motherID.ID  
                                            from Persons fatherID, Persons motherID 
                                            where fatherID.Name = 'Louis Vingo' 
                                              and motherID.Name = 'Dianne Vingo'))

错误:

在预期条件的上下文中指定的非布尔类型的表达式,靠近“,”。
')' 附近的语法不正确。

我读过的任何建议可能是由于在这种情况下代码仅采用一个参数?

使用您in语法,您需要两个条件:

where fatherID in (select p.ID from Persons p where p.Name = 'Louis Vingo') and
      motherID in (select p.ID from Persons p where p.Name = 'Dianne Vingo')

将它组合到一个子查询中是相当棘手的——而且不值得付出努力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM