MySQL simply implements multi-field fuzzy query

The original text is transferred from: http://blog.csdn.net/yaoyao9565/article/details/51305854

       

The mall project I have made has raised new requirements some time ago, requiring the front desk to search for products not only by product name, but also by other information, such as: product number, detailed content description, etc., similar to full-text search . The first thing that came to my mind was lucene, but the amount of changes to the code would be too large. . . . At present, if you make this kind of change in the online version, you are afraid that something will happen to the test. If you rebuild the table and store this information, it will be very troublesome. . . So I thought about making a fuss about the sql statement. Searched online and found a way. That is MySQL single-label multi-field fuzzy query.

 

MySQL single-table and multi-field fuzzy query refers to the realization of fuzzy query of multiple keywords on multiple fields in a single table, but these keywords do not necessarily exist in a certain field. Of course, you can also do this with multi-table association queries.

 

For example, the existing table table has three fields title, tag and description, which respectively record the title, label and introduction of a piece of data. Then, according to the query request input by the user, the input string is divided into multiple keywords by spaces, and then the records containing these keywords are queried in these three fields.

 

The problem encountered at present is that these keywords may exist in any one or more of the three fields, but the three fields must contain all the keywords. If fuzzy matching is performed on each field separately, it is impossible to achieve the required requirements, so I think of two methods:

 

When inserting a record, merge the fields that need to be used for MySQL single-table multi-field fuzzy query into a string and add it to a new field, and then perform a fuzzy query on this new field.
Use full-text search, but this requires Chinese word segmentation or conversion of Chinese characters into pinyin (splitting Chinese characters is not feasible, MySQL default FT minimum byte is 4), and it is not conducive to future maintenance.
How to use CONCAT in "MySQL Authoritative Guide", the description of CONCAT in the book is:

 

CONCAT(str1,str2,...)
Return value: A string obtained by combining all the input and output parameters. Returns NULL as long as there is a NULL value in the input parameter. CONCAT allows the case of only one input parameter.

Therefore, MySQL single-table multi-field fuzzy query can be implemented through the following SQL query

  1. SELECT * FROM `magazine` WHERE CONCAT(`title`,`tag`,`description`) LIKE ‘%关键字%’  


But there is a problem with this. If the value in these three fields is NULL, the returned value is also NULL, then this record may be missed. How to deal with it? I use IFNULL to judge, then the sql change for:

 

  1. <pre name="code" class="sql">SELECT * FROM `magazine` WHERE CONCAT(IFNULL(`title`,''),IFNULL(`tag`,''),IFNULL(`description`,'')) LIKE ‘%关键字%’  

Similar to this, a simple multi-field fuzzy search can be performed.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326608995&siteId=291194637