1、case when数据库
与 if - else 相似,语句以下:
CASE expr WHEN expr1 THEN return_expr1
[WHEN expr2 THEN return_expr2
...
WHEN exprn THEN return_exprn
ELSE else_expr]
ENDexpress
且有两种判断方法,case 字段 when 值 then return 值函数
else return 值 endspa
例如:code
select bname , price, case when price > =10 and price <20 then 'price1' blog
when price > =20 and price <30 then 'price2'
when price >= 30 and price <40 then 'price3'io
when price > =40 and price <50 then 'price4'select
when price >= 50 and price <60 then 'price5'方法
else 'price6' end "价格段"
from book;im
2、 decode (Oracle数据库独有)
DECODE(col|expression, search1, result1
[, search2, result2,...,]
...
[, searchn, resultn,...,]
[, default])
也能够和 sign函数一块儿使用
也能够:decode(字段,判断条件,返回值1,返回值2)
select decode(sign(arg1-arg2),-1, arg1, arg2) from dual;
注:sign()函数根据某个值是0、正数仍是负数,分别返回0、一、-1
select price,decode(price,'32.5','活着','其余' ) 书名 from book;
3、比较
1.DECODE 是Oracle特有的;
2.CASE WHEN 是Oracle, SQL Server,MySQL 均可用;
3.DECODE 只能用作相等判断,可是能够配合sign函数进行大于,小于,等于的判断;CASE可用于=,>=,<,<=,<>,is null,is not null 等的判断;
4.DECODE 使用其来比较简洁,CASE 虽然复杂但更为灵活。