是否有任何SQL格式的方法,允许在多列中查找一系列值。
我获得解决方案的常规做法是:
where
cost_price between my_budget_price and my_affordable_price
or
max_retail_price between my_budget_price and my_affordable_price
or
offer_price between my_budget_price and my_affordable_price
这里,相同的between ... values
子句在多列上重复。
我不想重复。
我正在寻找类似的东西:
where my_price in between ( cost_price, max_retail_price,
offer_price )
where ( cost_price, max_retail_price, offer_price )
between my_budget_price and my_affordable_price
答案 0 :(得分:0)
在Oracle中,您可以编写用户定义的函数,并使用输入参数执行您想要的任何逻辑。
即
and 1 = my_udf(param1, param2, param3, param4)
然后在udf
中做任何你想做的事 function my_udf(a varchar2, b varchar2, c varchar2, d varchar2) return number is
begin
if (any logic you want here) then
return 1;
else
return 2;
end if;
end;
或更具体地说:
select *
from some_table
where 1 = my_udf(cost_price, max_retail_price, offer_price,
my_budget_price, my_affordable_price)
并且该功能有时会像:
function my_udf(c number, r number, o number, l number, m number) return number is
begin
if (c >= l and c <= m) or
(r >= l and c <= m) or
(o >= l and c <= m) then
return 1;
else
return 2;
end if;
end;
这与:
相同select *
from some_table
where (
(cost_price >= my_budget_price and cost_price <= my_affordable_price)
or (max_retail_price >= my_budget_price and max_retail_price <= my_affordable_price)
or (offer_price >= my_budget_price and offer_price <= my_affordable_price)
)