Oralce 常用查询(树查询,case when...)
树查询:
--树查询即 select…start with…connect by…prior语法, prior的位置不同,查询方向不同,结果也不一样 --表结构,假设顶级id为1,有一子节点id为30. create table orgment( id varchar(32), --id name varchar(50),--名称 parentid varchar(32) --父级 ) --查找一个节点的所有子节点 select * from orgment t start with t.id=1 connect by t.parent=prior t.id; --查找一个节点的所有上级 select * from orgment t start with t.id=30 connect by prior t.parent=t.id;
case when:
--case when可以根据条件重设某个字段的返回值,注意不要丢掉end关键字 --一个条件 select t.Name, (case t.id when 1 then 'zhao' when 2 then 'qian' when 3 then 'sun' else 'li' end) from T t; --一个条件或多个条件 select case when t.flag=1 and t.date<=sysdate-2 then 1 else 0 end as new_flag from t t;
with语句
--with创建临时表 with tmp as (select t.id, t.name from student t) select * from tmp
729010328@qq.com
浙公网安备 33010602011771号