这是我的SQL查询:
SELECT c.*, CAST ( 0 as int ) Score
FROM Caregiver c JOIN Elderly e ON EXISTS
(
SELECT x.LanguageID FROM
(
SELECT 1 AS LanguageID WHERE e.Chinese = 1 UNION ALL
SELECT 2 AS LanguageID WHERE e.Malay = 1 UNION ALL
SELECT 3 AS LanguageID WHERE e.Tamil = 1 UNION ALL
SELECT 4 AS LanguageID WHERE e.English = 1 UNION ALL
SELECT 5 AS LanguageID WHERE e.Others = 1
)
x INTERSECT SELECT y.LanguageID FROM
(
SELECT 1 AS LanguageID WHERE c.Chinese = 1 UNION ALL
SELECT 2 AS LanguageID WHERE c.Malay = 1 UNION ALL
SELECT 3 AS LanguageID WHERE c.Tamil = 1 UNION ALL
SELECT 4 AS LanguageID WHERE c.English = 1 UNION ALL
SELECT 5 AS LanguageID WHERE c.Others = 1
)
y
)
WHERE e.NRIC=@nric2
AND c.CaregiverID != (SELECT CaregiverID FROM RequestPairing WHERE ReqID=@reqid2)
不起作用,因为子查询( SELECT CaregiverID FROM RequestPairing WHERE ReqID=@reqid2)
正在返回多个值。
我的目的是利用子查询排除主查询返回的某些行。
所以对此有任何解决方法吗?
答案 0 :(得分:3)
您可以将此条件更改为NOT EXISTS
:
WHERE e.NRIC=@nric2
AND NOT EXISTS (SELECT CaregiverID FROM RequestPairing
WHERE ReqID=@reqid2 AND CaregiverID = c.CaregiverID)
答案 1 :(得分:1)
我想你想要not in
:
WHERE e.NRIC=@nric2 and
c.CaregiverID not in (SELECT CaregiverID FROM RequestPairing WHERE ReqID=@reqid2)
答案 2 :(得分:0)
是。您可以在选择查询周围使用带有括号的!=
,而不是NOT IN
。