MySql replace like query

MySQL is more efficient than the like statement to write
locate
position
instr
find_in_set

Tags: locate instr find_in_set Category: MySQL
Have you been looking for a more efficient method than MySQL's LIKE statement, here are a few of them.

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

In fact, the two functions locate(position) and instr can be used instead

1. LOCATE statement
SELECT `column` from `table` where locate(‘keyword’, `condition`)>0

2. Or the alias position of locate
POSITION statement
SELECT `column` from `table` where position(‘keyword’ IN `condition`)

3. INSTR statement
SELECT `column` from `table` where instr(`condition`, ‘keyword’ )>0

The difference between locate, position and instr is only the position of the parameter is different, and the two are the same except that locate has one more starting position parameter.
mysql> SELECT LOCATE(‘bar’, ‘foobarbar’,5);

-> 7

In terms of speed, these three are slightly faster than using like.



~~~~~~~~~~~~~~~~~~~~~~~~ Gorgeous dividing line~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~

Fourth, I would like to introduce a new member to you, that is find_in_set

find_in_set(str1,str2) function: Returns the position index of str1 in str2, where str2 must be separated by ",".

surface:
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 statement
mysql> select * from test where find_in_set('name1',name);
+----+------------+
| id | name       |
+----+------------+
| 1  | name1,nam2 |
| 2  | name1      |
+----+------------+
2 rows in set (0.02 sec)

5. Of course, there is also the full-text index of mysql

Full text index: http://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326291242&siteId=291194637