简体   繁体   English

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

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

When I run the following code in SQL Server 2014 Express, it works fine to get all children of this father for a family relation table.当我在 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'))  

But when I try and check both the mother and father with this code I get an error.但是当我尝试用这段代码检查母亲和父亲时,我得到了一个错误。

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'))

Error:错误:

An expression of non-boolean type specified in a context where a condition is expected, near ','.在预期条件的上下文中指定的非布尔类型的表达式,靠近“,”。
Incorrect syntax near ')'. ')' 附近的语法不正确。

Any suggestions I have read that it may be due to the code only taking one parameter in this case?我读过的任何建议可能是由于在这种情况下代码仅采用一个参数?

Using your syntax, you need two in conditions:使用您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')

It is rather tricky to combine this into a single subquery -- and not worth the effort.将它组合到一个子查询中是相当棘手的——而且不值得付出努力。

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

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