我有一个存储过程,我正在使用子查询,但问题是我的子查询返回2个值,我将根据这两个值返回所有记录。
Select * from [Address] where AddressID=
(Select AddressID from PersonAddress where PersonID=
(select Claimant from [Case] where CaseID=35))
在这个查询中,AddressID返回两个值,并且两个值都有表中的记录,我将返回两个地址。
我该如何解决这个问题?
答案 0 :(得分:6)
而不是=
使用IN
:
Select * from [Address] where AddressID IN
(Select AddressID from PersonAddress where PersonID IN
(select Claimant from [Case] where CaseID=35))
或尝试JOIN
,正确的方法:
Select * from Address a
inner join PersonAddress p on a.AdressID = p.AddressID
inner join Case c on p.PersonID = c.Claimant
where c.CaseID = 35
答案 1 :(得分:0)
这样的事情:
SELECT
*
FROM
[Address]
WHERE EXISTS
(
SELECT
NULL
from
PersonAddress
WHERE
PersonAddress.AddressID=[Address].AddressID
AND EXISTS
(
SELECT
NULL
FROM
[Case]
WHERE
[Case].CaseID=35
AND PersonAddress.PersonID=[Case].Claimant
)
)
答案 2 :(得分:0)
您有两种选择:
使用像这样的IN子句:
选择* 来自[地址] 哪里 地址ID IN ( 选择AddressID 来自PersonAddress 其中PersonID IN(从案例ID = 35的[案例]中选择索赔人) )
或使用TOP子句
限制子查询从[地址]中选择*,其中AddressID = (从PersonAddress中选择TOP 1 AddressID,其中PersonID = (从案例ID = 35的[案例]中选择TOP 1索赔人))