sql-server-2016 - SQL:不能存在有“或”吗?
问题描述
我有一个不存在的 SQL 查询,用于从最终结果中排除一组值。
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
查询本身运行并返回值就好了。但是当我运行整个查询时,我没有得到任何结果。我怎样才能解决这个问题?
解决方案
SQL 具有明确定义的操作顺序。AND 子句在 OR 子句之前计算。因此,您的新查询将选择满足以下条件的记录:
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
推荐阅读
- eclipse - Eclipse EGit 未打开文件比较
- java - 从多模块maven项目的另一个模块下载代码的正确方法是什么?
- javascript - HTML5 - 文件名在媒体播放器中不起作用
- javascript - 如何在单选按钮上正确注册点击事件?
- php - 无法在 PHP 中获得正确的类型转换结果
- javascript - Date.toLocaleDateString() 在 Nodejs v10.14.2 上不起作用
- python - django - 陷入路径生成问题
- python - 使用“with tf.Session()”的目的是什么?
- odoo - Odoo 多列索引
- go - 在嵌套结构中获取数据的简单方法