1.最常用的 case when 语句
case 变量
when 条件1 then 语句1
when 条件2 then 语句2
when 条件3 then 语句3
else
else 语句
end;
select e_id,name,salary,
(case salary
when '200' then '经理'
when '304' then '主管'
when '200' then '经理'
else '总裁' end
)as position
from employeers
2.decode 语句
decode(变量名,条件1,结果1,条件2,结果2,else 最后一个条件的结果);
select e_id ,name,position ,
(decode(position,'经理',200,'主管',304,'部门经理',404,'老板',405,500)) as salary
from employeers;
3.常见的sql优化方式
下面这句sql的意思无论什么时候都插入数据
1. merge 语句
merge into t2
using(select * from t1)f
on (1=2)
when matched then
update set b=2 where a=t1 //当1=2 成立的时候 进行更新
when not matched then
insert(a,b)values(f.a,f.b)//当1=2不成立的时候插入
2.oracle中树形查找方式
start with 条件 connect by 条件 注意prior在那边查询查对应的东西
explain plan for select id,name from menu start with id=1000 connect by prior parent_id=id;
select id,name from menu start with id=1000 connect by prior parent_id= id;
select id,name from menu start with id=1002 connect by parent_id=prior id;
select id,name from menu start with id=1002 connect by prior parent_id=id;
select count(1) from(
select m.* from menu m
where id not in (select parent_id from menu)
);
and m.parent_id!=0;
这种用法的个人理解是查询 查询满足以满足start with 为基准,prior在左边则向上查找,在右边则向下查找
3.使用with as 语句优化sql的执行
with temp as (
select * from menu where pare
4.使用where 替代having 尽量过滤掉条件由于having 是对where 过滤后的进行分组, 所以使用where 减少数据条数可以提高效率。
5.大量数据处理应该放在where语句后面
6.用not exitsts 替换not in exist 替换in