Home > Database > Mysql Tutorial > sql instr()与LOCATE()字符串查找函数

sql instr()与LOCATE()字符串查找函数

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:51:21
Original
1485 people have browsed it

由于今天一网站的子分类用到了关于sql instr()与LOCATE()字符串操作函数,下面做个笔记放下来,有需要的朋友可以参考一下。

INSTR(str,substr)
返回字符串 str 中子字符串的第一个出现位置。这和LOCATE()的双参数形式相同,除非参数的顺序被颠倒。 

 代码如下 复制代码
> SELECT INSTR('foobarbar', 'bar');
        -> 4
mysql> SELECT INSTR('xbar', 'foobar');
        -> 0


LOCATE(substr,str) , LOCATE(substr,str,pos)
第一个语法返回字符串 str中子字符串substr的第一个出现位置。第二个语法返回字符串 str中子字符串substr的第一个出现位置, 起始位置在pos。如若substr 不在str中,则返回值为0。

 代码如下 复制代码
mysql> SELECT LOCATE('bar', 'foobarbar');
        -> 4
mysql> SELECT LOCATE('xbar', 'foobar');
        -> 0
mysql> SELECT LOCATE('bar', 'foobarbar',5);
        -> 7

这个函数支持多字节字元,并且只有当至少有一个参数是二进制字符串时区分大小写。


POSITION(substr IN str)
返回子串 substr 在字符串 str 中第一次出现的位置。如果子串 substr 在 str 中不存在,返回值为 0:

 代码如下 复制代码
mysql> SELECT POSITION('bar', ‘foobarbar');
-> 4
mysql> SELECT POSITION('xbar', ‘foobar');
-> 0

效率测试

 代码如下 复制代码

SELECT * FROM `o_soft` WHERE LOCATE('d200',tid2)>0

MySQL 返回的查询结果为空(即零行)。 (查询花费 0.0050 秒)

 代码如下 复制代码

SELECT * FROM `o_soft` WHERE INSTR('d200',tid2)>0

MySQL 返回的查询结果为空(即零行)。 (查询花费 0.0009 秒)

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
sql file
From 1970-01-01 08:00:00
0
0
0
php - Overhead of prepare vs sql?
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
Pass array to SQL insert query using PHP
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template