赵士懿,非诚勿扰11号女嘉宾,qq个性签名转换器
1. case when 表达式有两种形式
--简单case函数
case sex
when '1' then '男'
when '2' then '女'
else '其他' end
--case搜索函数
case
when sex = '1' then '男'
when sex = '2' then '女'
else '其他' end
2. case when 在语句中不同位置的用法
2.1 select case when 用法
select grade, count (case when sex = 1 then 1 /*sex 1为男生,2位女生*/
else null
end) 男生数,
count (case when sex = 2 then 1
else null
end) 女生数
from students group by grade;
2.2 where case when 用法
select t2.*, t1.*
from t1, t2
where (case when t2.compare_type = 'a' and
t1.some_type like 'nothing%'
then 1
when t2.compare_type != 'a' and
t1.some_type not like 'nothing%'
then 1
else 0
end) = 1
2.3 group by case when 用法
select
case when salary <= 500 then '1'
when salary > 500 and salary <= 600 then '2'
when salary > 600 and salary <= 800 then '3'
when salary > 800 and salary <= 1000 then '4'
else null end salary_class, -- 别名命名
count(*)
from table_a
group by
case when salary <= 500 then '1'
when salary > 500 and salary <= 600 then '2'
when salary > 600 and salary <= 800 then '3'
when salary > 800 and salary <= 1000 then '4'
else null end;
3.关于if-then-else的其他实现
3.1 decode() 函数
select decode(sex, 'm', 'male', 'f', 'female', 'unknown')
from employees;
貌似只有oracle提供该函数,而且不支持ansi sql,语法上也没case when清晰,个人不推荐使用。
3.2 在where中特殊实现
select t2.*, t1.*
from t1, t2
where (t2.compare_type = 'a' and t1.some_type like 'nothing%')
or
(t2.compare_type != 'a' and t1.some_type not like 'nothing%')
这种方法也是在特殊情况下使用,要多注意逻辑,不要弄错。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
Oracle入门学习五
学习视频: https://www.bilibili.com/video/BV1tJ411r7EC?p=55 数据的完整性:保证插入表格的数据必须正确。...
[阅读全文]
-
Oracle入门学习六
事务:把一组操作看做一个工作单元,要么都执行,要么都不执行。dml操作才有事务,查询没有事务。 开始事务:从上一次的事务结束之后,从第一次dml操作,就...
[阅读全文]
-
-
-
Oracle入门学习一
oracle的安装,用户授权,表格操作,数据类型,ddl表格,dml数据。 下一篇:Oracle入门学习二 学习视频:https://www.bilib...
[阅读全文]
-
Oracle入门学习二
上一篇:Oracle入门学习一 学习视频:https://www.bilibili.com/video/BV1tJ411r7EC?p=15 算术运算符:...
[阅读全文]
-
Oracle许可
原因:因为甲方比较关心许可的一些问题,会经常问起,但是现场人员往往对许可的内容不太了解,这里基于网络搜索的内容进行统一梳理。因为内容大多来自于网络,如果...
[阅读全文]
-
-
-
网友评论