SQL 查询返回在 Where 语句上过滤的不需要的数据
SQL Query Returning Unwanted Data Which was Filtered on Where Statement
我有以下查询:当我执行查询时,我得到很多 ad.Branch 的结果,包括 106804。如果我删除 BOR.SSN 和 COB.SSN喜欢,只是离开分支,它 returns 只有 106804 条记录,这是期望的结果。所以我相信 like 语句是导致问题的原因。
declare @Social varchar(5);
set @social = '%####'
SELECT
ABC.AccountNumber
FROM gcc.AccountDailyFact ABC
JOIN gcc.Today T ON ABC.CalendarDate = T.Today
JOIN gcc.ADReference AD ON ABC.ADReferenceRowId = AD.ADReferenceRowId
JOIN gcc.CDAccountData BOR ON ABC.BDLeaseHolderRowId = BOR.CDAccountDataRowId
JOIN gcc.CDAccountData COB ON ABC.CoBDLeaseHolderRowId = COB.CDAccountDataRowId
JOIN gcc.VDAssetFile VD ON ABC.VDAssetFileRowID = VD.VDAssetFileRowID
WHERE BOR.SSN like @social or COB.Ssn like @social and ad.Branch in
('106804')
我认为你只需要括号:
WHERE (BOR.SSN like @social or COB.Ssn like @social) and
ad.Branch in ('106804')
我有以下查询:当我执行查询时,我得到很多 ad.Branch 的结果,包括 106804。如果我删除 BOR.SSN 和 COB.SSN喜欢,只是离开分支,它 returns 只有 106804 条记录,这是期望的结果。所以我相信 like 语句是导致问题的原因。
declare @Social varchar(5);
set @social = '%####'
SELECT
ABC.AccountNumber
FROM gcc.AccountDailyFact ABC
JOIN gcc.Today T ON ABC.CalendarDate = T.Today
JOIN gcc.ADReference AD ON ABC.ADReferenceRowId = AD.ADReferenceRowId
JOIN gcc.CDAccountData BOR ON ABC.BDLeaseHolderRowId = BOR.CDAccountDataRowId
JOIN gcc.CDAccountData COB ON ABC.CoBDLeaseHolderRowId = COB.CDAccountDataRowId
JOIN gcc.VDAssetFile VD ON ABC.VDAssetFileRowID = VD.VDAssetFileRowID
WHERE BOR.SSN like @social or COB.Ssn like @social and ad.Branch in ('106804')
我认为你只需要括号:
WHERE (BOR.SSN like @social or COB.Ssn like @social) and
ad.Branch in ('106804')