【转】MySQL比like语句更高效的写法

https://blog.csdn.net/wpfphp/article/details/52584232

https://blog.csdn.net/shellching/article/details/8098120 

instr语法

编辑

语法如下:
  instr( string1, string2, start_position,nth_appearance ) [1]  [2] 

string1

源字符串,要在此字符串中查找。

string2

要在string1中查找的字符串 。

start_position

代表string1 的哪个位置开始查找。此参数可选,如果省略默认为1. 字符串索引从1开始。如果此参数为正,从左到右开始检索,如果此参数为负,从右到左检索,返回要查找的字符串在源字符串中的开始索引。

nth_appearance

代表要查找第几次出现的string2. 此参数可选,如果省略,默认为 1.如果为负数系统会报错。

 注意一点,locate语句后面设置<0, >0 , =0的区别:


mysql> select*from `runoob_tbl`;
+-----------+--------------+------------------+-----------------+
| runoob_id | runoob_title | runoob_author    | submission_date |
+-----------+--------------+------------------+-----------------+
|         1 | 学习 PHP     | 爆炸菜鸟教程爆炸 | 2018-08-15      |
|         2 | 学习 MySQL   | 菜鸟教程         | 2018-08-15      |
|         3 | 学习 C++     | RUNOOB.COM       | 2016-05-06      |
|         4 | JAVA 教程    | RUNOOB.COM       | 2016-03-06      |
|         5 | 连接         | 随便写           | 2018-08-18      |
+-----------+--------------+------------------+-----------------+


mysql> select*from `runoob_tbl` where locate('教程',`runoob_author`)>0;
+-----------+--------------+------------------+-----------------+
| runoob_id | runoob_title | runoob_author    | submission_date |
+-----------+--------------+------------------+-----------------+
|         1 | 学习 PHP     | 爆炸菜鸟教程爆炸 | 2018-08-15      |
|         2 | 学习 MySQL   | 菜鸟教程         | 2018-08-15      |
+-----------+--------------+------------------+-----------------+
2 rows in set

mysql> select*from `runoob_tbl` where locate('教程',`runoob_author`)<0;
Empty set

mysql> select*from `runoob_tbl` where locate('教程',`runoob_author`)=0;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
|         3 | 学习 C++     | RUNOOB.COM    | 2016-05-06      |
|         4 | JAVA 教程    | RUNOOB.COM    | 2016-03-06      |
|         5 | 连接         | 随便写        | 2018-08-18      |
+-----------+--------------+---------------+-----------------+
3 rows in set
  • 语句结构:
  • LOCATE(substr,str) , LOCATE(substr,str,pos
  • FIND_IN_SET(substr,strlist)

like是广泛的模糊匹配,字符串中没有分隔符,find_in_set是精确匹配,字段值以英文”,”分隔, 注意 find_in_set 是全表扫描的

例:

mysql> select*from `runoob_tbl`;
+-----------+--------------+------------------+-----------------+
| runoob_id | runoob_title | runoob_author    | submission_date |
+-----------+--------------+------------------+-----------------+
|         1 | 学习 PHP     | 爆炸菜鸟教程爆炸 | 2018-08-15      |
|         2 | 学习 MySQL   | 菜鸟教程         | 2018-08-15      |
|         3 | 学习 C++     | RUNOOB.COM       | 2016-05-06      |
|         4 | JAVA 教程    | RUNOOB.COM       | 2016-03-06      |
|         5 | 连接         | 随便写           | 2018-08-18      |
+-----------+--------------+------------------+-----------------+
5 rows in set

mysql> select*from `runoob_tbl` where find_in_set('菜鸟教程',`runoob_author`);
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
|         2 | 学习 MySQL   | 菜鸟教程      | 2018-08-15      |
+-----------+--------------+---------------+-----------------+
1 row in set
#find_in_set后跟完整的字符串'菜鸟教程'可选出结果。

否则,结果为空:
mysql> select*from `runoob_tbl` where find_in_set('教程',`runoob_author`)>0;
Empty set


若设置find_in_set=0,则选出的结果为不包含'菜鸟教程'的内容:

mysql> select*from `runoob_tbl` where find_in_set('菜鸟教程',`runoob_author`)=0;
+-----------+--------------+------------------+-----------------+
| runoob_id | runoob_title | runoob_author    | submission_date |
+-----------+--------------+------------------+-----------------+
|         1 | 学习 PHP     | 爆炸菜鸟教程爆炸 | 2018-08-15      |
|         3 | 学习 C++     | RUNOOB.COM       | 2016-05-06      |
|         4 | JAVA 教程    | RUNOOB.COM       | 2016-03-06      |
|         5 | 连接         | 随便写           | 2018-08-18      |
+-----------+--------------+------------------+-----------------+
4 rows in set
  • INSTR(str,substr)
  • POSITION(substr IN str)   其中substr为需要查找的字符串,str及strlist为查找范围。

关于大小写敏感问题:https://blog.csdn.net/qq_27682041/article/details/73647706

猜你喜欢

转载自blog.csdn.net/qq_42254088/article/details/81810887
今日推荐