MySQL中比like語句更高效的寫法locate position instr find_in_set,locatefind_in_set

來源:互聯網
上載者:User

MySQL中比like語句更高效的寫法locate position instr find_in_set,locatefind_in_set
你是否一直在尋找比MySQL的LIKE語句更高效的方法的,下面我就為你介紹幾種。

LIKE語句
SELECT `column` FROM `table` where `condition` like`%keyword%'

事實上,可以使用 locate(position) 和 instr這兩個函數來代替

LOCATE語句
SELECT `column` from `table` where locate(‘keyword’,`condition`)>0

或是 locate 的別名 position
POSITION語句
SELECT `column` from `table` where position(‘keyword’ IN`condition`)

或是
INSTR語句
SELECT `column` from `table` where instr(`condition`, ‘keyword’)>0

locate、position 和 instr 的差別只是參數的位置不同,同時locate多一個起始位置的參數外,兩者是一樣的。
mysql> SELECT LOCATE(‘bar’, ‘foobarbar’,5);

-> 7

速度上這三個比用 like 稍快了一點。



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~華麗的分割線~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
還要給大家介紹一個新成員,那就是find_in_set

find_in_set(str1,str2) 函數:返回str2中str1所在的位置索引,其中str2必須以","分割開。表:mysql> select * from region;+----+-------------+| id | name               |+----+-------------+|  1  | name1,nam2  ||  2  | name1             ||  3  | name3       ||  4  | name2,name4 ||  5  | name3,name5 |+----+-------------+5 rows in set (0.00 sec)FIND_IN_SET語句mysql> select * from test where find_in_set('name1',name);+----+------------+| id | name            |+----+------------+|  1  | name1,nam2 ||  2  | name1      |+----+------------+2 rows in set (0.02 sec)謝謝關注websites部落格.

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.