天天看點

MySQL 的instr函數

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)執行個體

格式一

1 select instr('helloworld','l') from dual; --傳回結果:3    預設第一次出現“l”的位置
2 select instr('helloworld','lo') from dual; --傳回結果:4    即:在“lo”中,“l”開始出現的位置
3 select instr('helloworld','wo') from dual; --傳回結果:6    即“w”開始出現的位置      

格式二

MySQL 的instr函數
1 select instr('helloworld','l',2,2) from dual;  --傳回結果:4    也就是說:在"helloworld"的第2(e)号位置開始,查找第二次出現的“l”的位置
2 select instr('helloworld','l',3,2) from dual;  --傳回結果:4    也就是說:在"helloworld"的第3(l)号位置開始,查找第二次出現的“l”的位置
3 select instr('helloworld','l',4,2) from dual;  --傳回結果:9    也就是說:在"helloworld"的第4(l)号位置開始,查找第二次出現的“l”的位置
4 select instr('helloworld','l',-1,2) from dual;  --傳回結果:4    也就是說:在"helloworld"的倒數第1(d)号位置開始,往回查找第二次出現的“l”的位置
5 select instr('helloworld','l',-2,2) from dual;  --傳回結果:4    和第四個一樣
6 select instr('helloworld','l',2,3) from dual;  --傳回結果:9    也就是說:在"helloworld"的第2(e)号位置開始,查找第三次出現的“l”的位置
7 select instr('helloworld','l',-2,3) from dual; --傳回結果:3    也就是說:在"helloworld"的倒數第2(l)号位置開始,往回查找第三次出現的“l”的位置      
MySQL 的instr函數

   參考:​​MySQL|INSTR() 函數用法 ​​

注:MySQL中的模糊查詢 like 和oracle中的instr()函數有同樣的查詢效果; 如下所示:   

select * from tableName a where name like '%helloworld%';
select * from tableName a where instr(name,'helloworld')>0;  --這兩條語句的效果是一樣的      

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)執行個體

格式一

1 select instr('helloworld','l') from dual; --傳回結果:3    預設第一次出現“l”的位置
2 select instr('helloworld','lo') from dual; --傳回結果:4    即:在“lo”中,“l”開始出現的位置
3 select instr('helloworld','wo') from dual; --傳回結果:6    即“w”開始出現的位置      

格式二

MySQL 的instr函數
1 select instr('helloworld','l',2,2) from dual;  --傳回結果:4    也就是說:在"helloworld"的第2(e)号位置開始,查找第二次出現的“l”的位置
2 select instr('helloworld','l',3,2) from dual;  --傳回結果:4    也就是說:在"helloworld"的第3(l)号位置開始,查找第二次出現的“l”的位置
3 select instr('helloworld','l',4,2) from dual;  --傳回結果:9    也就是說:在"helloworld"的第4(l)号位置開始,查找第二次出現的“l”的位置
4 select instr('helloworld','l',-1,2) from dual;  --傳回結果:4    也就是說:在"helloworld"的倒數第1(d)号位置開始,往回查找第二次出現的“l”的位置
5 select instr('helloworld','l',-2,2) from dual;  --傳回結果:4    和第四個一樣
6 select instr('helloworld','l',2,3) from dual;  --傳回結果:9    也就是說:在"helloworld"的第2(e)号位置開始,查找第三次出現的“l”的位置
7 select instr('helloworld','l',-2,3) from dual; --傳回結果:3    也就是說:在"helloworld"的倒數第2(l)号位置開始,往回查找第三次出現的“l”的位置      
select * from tableName a where name like '%helloworld%';
select * from tableName a where instr(name,'helloworld')>0;  --這兩條語句的效果是一樣的