2017-02-10 168 views
0

當我執行唯一工作的是Zipcodes。它不會給我的服務日期大於08/01/16或我的結算提供商。我不知道爲什麼。爲什麼我的代碼被忽略?

SELECT Name, BirthDate, Address1, Address2, City, StateProvince, ZipCode, BillingProviderID, ServiceDate, ChargeCatID 
FROM dbo.PbrChargeTransactions 
WHERE ChargeCatID = 'EM' 
AND ServiceDate >= '08/01/16' 
AND BillingProviderID IN ('AAD.FD','DSD.DFD','ASDF.DD') 
AND ZipCode Like '68730%' 
OR ZipCode Like '68792%' 
OR ZipCode Like '68739%' 
OR ZipCode Like '68718%' 
OR ZipCode Like '57069%' 
OR ZipCode Like '57031%' 
OR ZipCode Like '57078%' 
OR ZipCode Like '57066%' 
OR ZipCode Like '57063%' 
OR ZipCode Like '57037%' 
OR ZipCode Like '57073%' 
OR ZipCode Like '57029%' 
OR ZipCode Like '57070%' 
ORDER BY Name 
+2

你需要一些括號:現在,你得到(ChargeCatID ='EM'和ServiceDate> = '08/01/16'和BillignProviderID IN(...)和ZipCode Like'68730%')或者(ZipCode像'68792%')或(ZipCode贊...)。顯然,那不是你想要的。 – Martha

回答

1

運算符優先級:「和」的綁定比「或」更緊密。也就是說,只有第一個郵政編碼比較用您的「和」條件的其餘部分進行評估。其餘的都是'OR',如果他們匹配,你會得到記錄。

試試這個:

SELECT Name, BirthDate, Address1, Address2, City, StateProvince, ZipCode,  BillingProviderID, ServiceDate, ChargeCatID 
FROM dbo.PbrChargeTransactions 
WHERE ChargeCatID = 'EM' 
AND ServiceDate >= '08/01/16' 
AND BillingProviderID IN ('AAD.FD','DSD.DFD','ASDF.DD') 
AND (ZipCode Like '68730%' 
OR ZipCode Like '68792%' 
OR ZipCode Like '68739%' 
OR ZipCode Like '68718%' 
OR ZipCode Like '57069%' 
OR ZipCode Like '57031%' 
OR ZipCode Like '57078%' 
OR ZipCode Like '57066%' 
OR ZipCode Like '57063%' 
OR ZipCode Like '57037%' 
OR ZipCode Like '57073%' 
OR ZipCode Like '57029%' 
OR ZipCode Like '57070%' 
) 
ORDER BY Name 
0

您可以使用(...)作爲n8wrl表演或以下,我認爲會更快。

SELECT Name, BirthDate, Address1, Address2, City, StateProvince, ZipCode, BillingProviderID, ServiceDate, ChargeCatID 
FROM dbo.PbrChargeTransactions 
WHERE ChargeCatID = 'EM' 
AND ServiceDate >= '08/01/16' 
AND BillingProviderID IN ('AAD.FD','DSD.DFD','ASDF.DD') 
AND LEFT(ZipCode,5) in ('68730','68792','68739','68718','57069','57031', '57078','57066','57063','57037','57073','57029','57070') 
ORDER BY Name 
0

這個問題可能是因爲你錯過了一些括號。請注意的precedence of operators

如果你寫

A AND B AND zip1 OR zip2 OR zip3 

這等於

((A AND B) AND zip1) OR zip2 OR zip3 

我想你打算寫

A AND B AND (zip1 OR zip2 OR zip3) 

其中A,B,ZIP1 ,...是布爾條件,如zip LIKE '12345%'