Oracle函数之---- instr,decode

 

1)instr()函数的格式 (俗称:字符查找函数)

格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串)

格式二:instr( string1, string2 [, start_position [, nth_appearance ] ] ) / instr(源字符串, 目标字符串, 起始位置, 匹配序号)

解析:string2 的值要在string1中查找,是从start_position给出的数值(即:位置)开始在string1检索,检索第nth_appearance(几)次出现string2。

注:在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置。只检索一次,也就是说从字符的开始到字符的结尾就结束。

 

链接

 

2)decode函数

decode函数嵌套

 

 

【例】表中A,B,C三列,实现当A列大于B列时,选择A列,否则选择B列,当B列大于C列时,选择B列,否者选择C列

 select decode(sign(A-B),1,A,decode(sign(B-C),1,B,C)) from table;

 

 

【例】A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引。

方法一

  使用 not in ,容易理解,效率低  ~执行时间为:1.395秒~

select distinct a.id from a where a.id not in(select id from b)

方法二

  使用 left join...on... , "B.ID isnull" 表示左连接之后在B.ID 字段为 null的记录  ~执行时间:0.739秒~

select a.id from a left join b on a.id=b.id where b.id is null

 

方法三

  逻辑相对复杂,但是速度最快

select * from a where (select count(1) as num from b where a.id=b.id)=0
posted @ 2018-02-09 15:03  雨 燕  阅读(396)  评论(0编辑  收藏  举报