Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解决SQL Server语句中括号未同时应用这两个条件的最佳做法是什么?_Sql_Sql Server_Tsql - Fatal编程技术网

解决SQL Server语句中括号未同时应用这两个条件的最佳做法是什么?

解决SQL Server语句中括号未同时应用这两个条件的最佳做法是什么?,sql,sql-server,tsql,Sql,Sql Server,Tsql,这个WHERE子句返回数据,就好像括号不在那里一样。使用“批发”或“餐厅”返回数据的最佳做法是什么,但不能同时使用这两种方法 SELECT fruit, price, market FROM fruit_data WHERE fruit = 'banana' AND (vendor != 'wholesale' AND market != 'restaurant') 我认为我的解决方案不是最佳实践,尽管它可以: AND ( (vendor != 'wholesa

这个WHERE子句返回数据,就好像括号不在那里一样。使用
“批发”
“餐厅”
返回数据的最佳做法是什么,但不能同时使用这两种方法

SELECT fruit, price, market
FROM fruit_data
WHERE
   fruit = 'banana'
   AND (vendor != 'wholesale' AND market != 'restaurant')
我认为我的解决方案不是最佳实践,尽管它可以:

   AND (
       (vendor != 'wholesale' AND market = 'restaurant')
       OR (vendor = 'wholesale' AND market != 'restaurant')
       OR (vendor != 'wholesale' AND market != 'restaurant')
       )

您的
where
子句与

fruit = 'banana' AND vendor <> 'wholesale' AND market <> 'restaurant'

您的
where
子句与

fruit = 'banana' AND vendor <> 'wholesale' AND market <> 'restaurant'
…包含“批发”或“餐厅”的数据,但不能同时包含两者

经典方法:

select      fruit, price, market
from        fruit_data
where       fruit = 'banana'
and         (vendor = 'wholesale' or market = 'restaurant')
and         not (vendor = 'wholesale' and market = 'restaurant')
select      fruit, price, market
from        fruit_data
cross apply (select 
                isWs = iif(vendor = 'wholesale', 1, 0),
                isRst = iif(market = 'restaraunt', 1, 0)
            ) ap
where       fruit = 'banana'
and         isWs + isRst = 1;
更为算术的方法:

select      fruit, price, market
from        fruit_data
where       fruit = 'banana'
and         (vendor = 'wholesale' or market = 'restaurant')
and         not (vendor = 'wholesale' and market = 'restaurant')
select      fruit, price, market
from        fruit_data
cross apply (select 
                isWs = iif(vendor = 'wholesale', 1, 0),
                isRst = iif(market = 'restaraunt', 1, 0)
            ) ap
where       fruit = 'banana'
and         isWs + isRst = 1;
…包含“批发”或“餐厅”的数据,但不能同时包含两者

经典方法:

select      fruit, price, market
from        fruit_data
where       fruit = 'banana'
and         (vendor = 'wholesale' or market = 'restaurant')
and         not (vendor = 'wholesale' and market = 'restaurant')
select      fruit, price, market
from        fruit_data
cross apply (select 
                isWs = iif(vendor = 'wholesale', 1, 0),
                isRst = iif(market = 'restaraunt', 1, 0)
            ) ap
where       fruit = 'banana'
and         isWs + isRst = 1;
更为算术的方法:

select      fruit, price, market
from        fruit_data
where       fruit = 'banana'
and         (vendor = 'wholesale' or market = 'restaurant')
and         not (vendor = 'wholesale' and market = 'restaurant')
select      fruit, price, market
from        fruit_data
cross apply (select 
                isWs = iif(vendor = 'wholesale', 1, 0),
                isRst = iif(market = 'restaraunt', 1, 0)
            ) ap
where       fruit = 'banana'
and         isWs + isRst = 1;
“批发”或“餐厅”数据,但不是两者都有

SELECT fruit, price, market
FROM fruit_data
WHERE
   fruit = 'banana'
   AND (vendor != 'wholesale' AND market != 'restaurant')
这不是:

vendor='wholesale'或market='restaurant'和(而不是(vendor='wholesale'和market='restaurant'))

“批发”或“餐厅”数据,但不是两者都有

SELECT fruit, price, market
FROM fruit_data
WHERE
   fruit = 'banana'
   AND (vendor != 'wholesale' AND market != 'restaurant')
这不是:

vendor='wholesale'或market='restaurant'和(NOT(vendor='wholesale'和market='restaurant'))

“这个WHERE子句返回数据,就好像括号不在那里一样。”这是值得怀疑的,你的理解是错误的。你想在这里干什么?样本数据和预期结果将帮助我们帮助您。您的“解决方案”看起来相当于
而不是(供应商='批发'和市场='餐厅')
和(供应商!='批发'或市场!='餐厅')
。拿你的选择来说。“这个WHERE子句返回数据,就好像括号不在那里一样。”这是值得怀疑的,这是你理解错误的地方。你想在这里干什么?样本数据和预期结果将帮助我们帮助您。您的“解决方案”看起来相当于
而不是(供应商='批发'和市场='餐厅')
和(供应商!='批发'或市场!='餐厅')
。你挑吧。