25. instr用法
2015-01-05 15:46
113 查看
很多时候,我们要进行字符串匹配,在SQL语句中,我们通常使用like来达到我们搜索的目标。但经过实际测试发现,like的效率与instr函数差别相当大。下面是一些测试结果:
select instr('abcd','a') from dual; --返回1
select instr('abcd','c') from dual; --返回3
select instr('abcd','e') from dual; --返回0
该函数可以用于模糊查询以及判断包含关系:
例如:
① select code, name, dept, occupation from staff where instr(code, '001') > 0;
等同于
select code, name, dept, occupation from staff where code like '%001%' ;
② select ccn,mas_loc from mas_loc where instr('FH,FHH,FHM',ccn)>0;
等同于
select ccn,mas_loc from mas_loc where ccn in ('FH','FHH','FHM');
SQL> select count(*) from t where instr(title,‟oracle‟)>0; COUNT(*) ———- 5478
Elapsed: 00:00:11.04
SQL> select count(*) from t where title like ”%oracle%‟; COUNT(*) ———- 5478
Elapsed: 00:00:31.47
注:
instr(title,'oracle‟)>0 相当于like
instr(title,'oracle‟)=0 相当于not like
--1.从第3个字符开始搜索
SQL> select instr('oracleor','or', 3) position from dual;
POSITION
----------
7
--2.从第1个字符开始,搜索第2次出现子串的位置
SQL> select instr('oracleor','or', 1, 2) position from dual;
POSITION
----------
7
--3.从倒数第1个字符开始,搜索第1次出现子串的位置
SQL> select instr('oracleor','or', -1, 1) position from dual;
POSITION
----------
7
--4.从倒数第1个字符开始,搜索第2次出现子串的位置
SQL> select instr('oracleor','or', -1, 2) position from dual;
POSITION
----------
1
经典例子:
select * from tuser a where instr(','||(select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300)||',',','||a.id||',')>0;
(select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300) 结果为字符串:3993,451,1889,1482
如果直接这么写:select * from tuser a where a.id in (select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300)会报无效数字
select instr('abcd','a') from dual; --返回1
select instr('abcd','c') from dual; --返回3
select instr('abcd','e') from dual; --返回0
该函数可以用于模糊查询以及判断包含关系:
例如:
① select code, name, dept, occupation from staff where instr(code, '001') > 0;
等同于
select code, name, dept, occupation from staff where code like '%001%' ;
② select ccn,mas_loc from mas_loc where instr('FH,FHH,FHM',ccn)>0;
等同于
select ccn,mas_loc from mas_loc where ccn in ('FH','FHH','FHM');
SQL> select count(*) from t where instr(title,‟oracle‟)>0; COUNT(*) ———- 5478
Elapsed: 00:00:11.04
SQL> select count(*) from t where title like ”%oracle%‟; COUNT(*) ———- 5478
Elapsed: 00:00:31.47
注:
instr(title,'oracle‟)>0 相当于like
instr(title,'oracle‟)=0 相当于not like
--1.从第3个字符开始搜索
SQL> select instr('oracleor','or', 3) position from dual;
POSITION
----------
7
--2.从第1个字符开始,搜索第2次出现子串的位置
SQL> select instr('oracleor','or', 1, 2) position from dual;
POSITION
----------
7
--3.从倒数第1个字符开始,搜索第1次出现子串的位置
SQL> select instr('oracleor','or', -1, 1) position from dual;
POSITION
----------
7
--4.从倒数第1个字符开始,搜索第2次出现子串的位置
SQL> select instr('oracleor','or', -1, 2) position from dual;
POSITION
----------
1
经典例子:
select * from tuser a where instr(','||(select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300)||',',','||a.id||',')>0;
(select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300) 结果为字符串:3993,451,1889,1482
如果直接这么写:select * from tuser a where a.id in (select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300)会报无效数字
相关文章推荐
- 取得传值的函数和input标签的高级限制级用法
- static变量和static函数的用法摘抄
- 栈的常见递归用法
- 在delphi中paramstr用法
- java File类 简单用法举例
- C#判断当前激活的是哪个程序(DllImport用法说明)
- ServletContextListener的用法
- 数据库备份mysqldump的用法.
- android:addStatesFromChildren用法简单介绍
- Java_动态数组ArrayList基本用法
- ASP.NET----GridView多种用法,以及在后台取对应列的值
- RichEdit的用法总结
- Toast的基本用法
- case when和decode的用法与区别
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- c++之指针用法总结
- <C#>new几种用法
- const关键字用法
- stl之string类用法详细总结
- java中return语句的用法总结