SQL:NOT EXISTS 不能有 "OR"?

SQL: Can NOT EXISTS have "OR"?

我有一个 SQL 查询 NOT EXISTS 从最终结果中排除一组特定的值。

select distinct 
from [rpt_StockInventorySummary] a
where a.[DepartmentId] ='P'
and not exists (
select *
from rpt_StockInventorySummary b
where b.DepartmentId = 'p' 
and b.Manufacturer = 'warrington'
and b.LowestGroup = 57 and b.Instock = 0
and b.Barcode = a.Barcode
)
order by a.SortOrder

查询工作正常,但现在我需要修改 SQL,以便从最终结果中排除另一组值。所以我尝试像这样修改 NOT EXISTS 中的 SQL。

select *
from rpt_StockInventorySummary b
where b.DepartmentId = 'p' 
and b.Manufacturer = 'warrington'
and (b.LowestGroup = 57 and b.Instock = 0)
or b.LowestGroup = 60
and b.Barcode = a.Barcode

本身,查询 运行s 和 returns 值就好了。但是当我 运行 整个查询时,我没有得到任何结果。我该如何解决这个问题?

SQL 具有明确定义的操作顺序。 AND 子句在 OR 子句之前计算。因此,您的新查询将 select 记录满足以下条件的情况:

b.DepartmentId = 'p' 
and b.Manufacturer = 'warrington'
and (b.LowestGroup = 57 and b.Instock = 0)

b.LowestGroup = 60
and b.Barcode = a.Barcode

我想以下内容可以满足您的需求:

select *
from rpt_StockInventorySummary b
where b.DepartmentId = 'p' 
and b.Manufacturer = 'warrington'
and (b.LowestGroup = 57 and b.Instock = 0 or b.LowestGroup = 60)
and b.Barcode = a.Barcode