Introduction of Fuzzy Query Method Using instr in mysql

  • 2021-09-16 08:27:07
  • OfStack

In mysql, the internal function instr can replace the traditional like query, and the speed is faster.

instr function, the first parameter is the field, the second parameter is the string to query, return the position of the string, the first is 1, if not found is 0.

For example, to query the field name with the name "Army", the traditional method is:


select name from  User table  where name like `% Army %';

With the method of instr:


select name from  User table  where instr('name ' , 'Army ');

Or:


select name from  User table  where instr('name ' ,' Army ')>0;

Table A

Field: Name

Zhang 3
Wang 5

Table B

Field: title

One piece of information and three releases
Information 2 King 5 Release
Three pieces of information are released

The ranking list is sorted according to the name of table A like% 'name'% matching the number of title of table B.

select name, count (b. title) from a inner join b on instr (b. title, a name) > 0 group by Name order by count (b. title)

Summarize

The above is this article about mysql using instr fuzzy query method introduced the whole content, I hope to help you. Interested friends can continue to refer to this site: Detailed explanation of MySQL data type DECIMAL (N, M) N and M respectively, Mysql FIND_IN_SET () and IN difference analysis, etc., any questions can leave a message at any time, this site will reply to you in time. Thank you friends for your support to this site!


Related articles: