【发布时间】:2015-01-30 08:06:27
【问题描述】:
我有两个表(fruits 和 fruitSales),我的查询要求是:
if fruit = 'Apple' OR 'Banana' then fire (query1) else fire(query2)
// 即,当我的输入是苹果或香蕉时,query1 必须触发,否则 query2。
这是我的两个查询:
查询 #1:
select a.*, b.quantity
from fruits a
left join fruitSales b on a.fruitPrice = '%'+b.fruitPrice+'%'
where a.fruit = 'Apple'
查询 #2:
select a.*, b.quantity
from fruits a
left join fruitSales b on a.fruitPrice like '%' + b.fruitPrice + '%'
where a.fruit = 'Orange'
简而言之:我的查询中的唯一区别是 query2 中的“like”和 query1 中的“=”。在这种情况下,我不知道如何使用 CASE 查询。(因为我的输入数据依赖于两个值,Apple 或 Banana)
非常感谢您的解决方案。
【问题讨论】:
-
为什么你不尝试 if else then case 语句 where as if else 在某些情况下被优化器处理得更好。 if (@fruit ='Apple' ) then begin your query ........ end if (@fruit ='Banana' ) then begin your query ........ end
-
我可以为两个输入做到这一点吗?如果(@fruit =Apple 或 @fruit=Banana)?这样的事情存在吗?
标签: sql sql-server select case sql-like