解决SQL Server语句中括号未同时应用这两个条件的最佳做法是什么?
这个WHERE子句返回数据,就好像括号不在那里一样。使用解决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
“批发”
或“餐厅”
返回数据的最佳做法是什么,但不能同时使用这两种方法
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子句返回数据,就好像括号不在那里一样。”这是值得怀疑的,这是你理解错误的地方。你想在这里干什么?样本数据和预期结果将帮助我们帮助您。您的“解决方案”看起来相当于而不是(供应商='批发'和市场='餐厅')
或和(供应商!='批发'或市场!='餐厅')
。你挑吧。