簡體   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