为什么我的代码被忽略了?

时间:2017-02-10 21:46:43

标签: sql sql-like

当我执行时,唯一有效的是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

3 个答案:

答案 0 :(得分:1)

运算符优先级:“和”绑定比“或”更紧密。也就是说,只有第一个邮政编码比较与其他“和”条件一起评估。其余的是“或”,如果匹配,你就会得到记录。

试试这个:

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

答案 1 :(得分: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

答案 2 :(得分: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%'