Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.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
Mysql 在多列中查找值的范围_Mysql_Sql_Oracle - Fatal编程技术网

Mysql 在多列中查找值的范围

Mysql 在多列中查找值的范围,mysql,sql,oracle,Mysql,Sql,Oracle,在任何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_affor

在任何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
在这里,相同的
在。。。values
子句在多个列上重复。 我不想重复了

我在找这样的东西:

  • 我的价格介于两者之间(成本价格、最高零售价格、,
    优惠价格)
  • where(成本价、最高零售价、报价)
    在我的预算价格和我的可承受价格之间

  • 在Oracle中,您可以编写一个用户定义的函数,并使用输入参数执行您想要的任何逻辑

    然后在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;
    
    函数my_udf(c编号、r编号、o编号、l编号、m编号)返回编号为
    开始
    
    如果(c>=l和c=l和c=l和c=my_budget_price和cost_price=my_budget_price和max_retail_price=my_budget_price和offer_price我不这么认为。你的东西有什么问题吗?@老程序员:没什么问题。我只是试图停止重复比较条件。然后,函数应该类似于
    f名称(col\u names\u array[],最小边界,最大边界)
    是的……我刚刚用一个更具体的例子更新了答案。希望这有帮助:)
    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)
          )