http://cindysaj.iteye.com/blog/296635
SELECT col1, col2,
CASE
WHEN col3 > 1 AND col3 <2
THEN '1'
WHEN col3 > 2 AND col3 <3
THEN '2'
WHEN col3 > 3 AND col3 <4
THEN '3'
ELSE '4'
END mylevel
FROM table1
注意点:
1、以CASE开头,以END结尾
2、分支中WHEN 后跟条件,THEN为显示结果
3、ELSE 为除此之外的默认情况,类似于高级语言程序中switch case的default,可以不加
4、END 后跟别名
关于insert语句的when case用法
insert into bb_recommend_tunnel_type_t(tunnel_id,tunnel_status)
values
(
case
when 1=0 then 'aaa'
else 'bbb'
end,case
when 1=0 then 0
else 1
end
);