oracle SQL 中的 if-elseif-else '条件'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15022392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:50:17  来源:igfitidea点击:

if-elseif-else 'condition' in oracle SQL

sqloracle

提问by user964147

I am wondering if there if possibility to achieve some thing like

我想知道是否有可能实现诸如

'if-elseif-else' condition , i know there is a 'case-when-then-else' but it checks only one condition at a time (if i understand it correctly). How can i achieve if-elseif-else scenario in Oracle sql

'if-elseif-else' 条件,我知道有一个 'case-when-then-else' 但它一次只检查一个条件(如果我理解正确的话)。我如何在 Oracle sql 中实现 if-elseif-else 场景

回答by ZKK

You can use if/else using case statements like this.

您可以使用 if/else 这样的 case 语句。

SELECT ename, CASE WHEN sal = 1000 THEN 'Minimum wage'
                   WHEN sal > 1000 THEN 'Over paid'
                   ELSE 'Under paid'
              END AS "Salary Status"
FROM   emp;

回答by DazzaL

i know there is a 'case-when-then-else' but it checks only one condition at a time

我知道有一个“case-when-then-else”,但它一次只检查一个条件

What you are describing is a SIMPLE case. Oracle has two case types: SIMPLE and SEARCHED (see here for more info http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm)

您所描述的是一个简单的案例。Oracle 有两种案例类型:SIMPLE 和 SEARCHED(有关更多信息,请参见此处http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm

SIMPLE

简单的

case A
  when 1 then 'foo'
  when 2 then 'bar'
  else ..
end

SEARCHED

已搜索

case
  when A=1 and B='A' then 'foo'
  when D + C =1 and B !='A' then 'Bar'
  else ..
end

you probably want to use a searched case. You can use them in PL/SQL or SQL. eg in SQL

您可能想要使用搜索过的案例。您可以在 PL/SQL 或 SQL 中使用它们。例如在 SQL 中

select ..
  from table
 where case
         when A=1 and B='A' then 'foo'
         when D + C =1 and B !='A' then 'Bar'
         else ..
       end = 'foo'

回答by asifsid88

Look out for "Decode in Oracle"

留意“在 Oracle 中解码”

decode( expression , search , result [, search , result]... [, default] )

It is similar to "if-elseif-else"

它类似于“if-elseif-else”

Refer: http://www.techonthenet.com/oracle/functions/decode.php

参考:http: //www.techonthenet.com/oracle/functions/decode.php

NOTE:It does only equality checks..

注意:它只做相等性检查..