The realization method of fuzzy query of Mysql single table multi-key word _mysql

Source: Internet
Author: User
A recent project needs to be implemented in a MySQL single table with multiple keyword fuzzy queries, but these keywords do not necessarily all exist in a field. For example, the existing table, which has title,tag,description three fields, records the title, label, and description of a piece of data respectively. Then, according to the query request entered by the user, the input string is separated into several keywords by a space, and then the records containing these keywords are queried in these three fields.


The problem that can be encountered today is that these keywords may be in any one or more of the three fields, but require three fields to contain all the keywords. If you have a fuzzy match for each field, you cannot achieve the required requirements, and then think of two ways:

When you insert a record, combine the fields that require a multiple-field fuzzy query into a single string and add it to a new field, and then make a fuzzy query on the new field. Use Full-text search, but this requires the use of Chinese participle or the conversion of Chinese characters to Pinyin (split Chinese characters is not feasible, mysql default ft minimum byte of 4), and is not conducive to future maintenance.
Climbed on the internet for two days, the treatment of this problem did not find a satisfactory solution, and finally in the "MySQL Authority Guide" to the use of concat, in the book on the concat description is:

CONCAT (STR1,STR2,... )
Return value: A string that is obtained by merging the parameters of the entire entry and exit. Returns null whenever a parameter in the input has a null value. Concat allow only one input parameter.

Therefore, the MySQL single table multi-keyword fuzzy query can be implemented through the following SQL query
SELECT * from ' magazine ' WHERE CONCAT (' title ', ' tag ', ' description ') is like '% keyword '
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.