【轉】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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章