Oracle中条件表达式(if-else if-else)有两种表达方法:
1.–CASE 表达式:SQL99的语法,类似Basic,比较繁琐
2.–DECODE 函数:Oracle自己的语法,类似Java,比较简单
CASE的用法:
例如:oracle中 emp员工表:
SQL> select ename,job,sal 涨前,
2 case job when 'PRESIDENT' then sal+1000
3 when 'MANAGER' then sal+800
4 else sal+400
5 end 涨后
6 from emp;
结果展示:
ENAME JOB 涨前 涨后
---------- --------- ---------- ----------
SMITH CLERK 800 1200
ALLEN SALESMAN 1600 2000
WARD SALESMAN 1250 1650
JONES MANAGER 2975 3775
MARTIN SALESMAN 1250 1650
BLAKE MANAGER 2850 3650
CLARK MANAGER 2450 3250
SCOTT ANALYST 3000 3400
KING PRESIDENT 5000 6000
TURNER SALESMAN 1500 1900
ADAMS CLERK 1100 1500
ENAME JOB 涨前 涨后
---------- --------- ---------- ----------
JAMES CLERK 950 1350
FORD ANALYST 3000 3400
MILLER CLERK 1300 1700
已选择 14 行。
DECODE用法:
例如:依然使用oracle中表格的数据:
SQL> select ename,job,sal 涨前,
2 decode(job,'PRESIDENT',sal+1000,
3 'MANAGER',sal+800,
4 sal+400) 涨后
5 from emp;
结果展示:
ENAME JOB 涨前 涨后
---------- --------- ---------- ----------
SMITH CLERK 800 1200
ALLEN SALESMAN 1600 2000
WARD SALESMAN 1250 1650
JONES MANAGER 2975 3775
MARTIN SALESMAN 1250 1650
BLAKE MANAGER 2850 3650
CLARK MANAGER 2450 3250
SCOTT ANALYST 3000 3400
KING PRESIDENT 5000 6000
TURNER SALESMAN 1500 1900
ADAMS CLERK 1100 1500
ENAME JOB 涨前 涨后
---------- --------- ---------- ----------
JAMES CLERK 950 1350
FORD ANALYST 3000 3400
MILLER CLERK 1300 1700
已选择 14 行。