MySQL multi-Keyword multi-field fuzzy query

Source: Internet
Author: User
Assume there are two types of data: (Table Name: user) 1) usernameadmin, password0000002) usernameadmin, password123456.

Assume there are two types of data: (Table Name: user) 1) username = admin, password = 000000 2) username = admin, password = 123456

Assume there are two data types:

(Table Name: user)

1) username = admin, password = 000000

2) username = admin, password = 123456

The effect we want to achieve is that you can enter multiple keywords for query. Multiple keywords are separated by commas.

Use the preceding table as an example: Enter the keyword "admin" to check the two data items. Enter "admin, 000000" to check only the first data. The SQL statement is as follows:

Select * from user where concat (username, password) like '% admin % ';

Select * from user where concat (username, password) like '% admin %' and concat (username, password) like '% 100 ';

Concat is used to connect strings, but there is a problem: If you enter the single keyword "admin000000", the first data will also be found. This is obviously not the result we want. The solution is: because multiple keywords are separated by commas, the comma will never be part of the keyword. Therefore, when connecting strings, we can separate each field with commas to solve this problem, the following SQL statement does not query the first data:

Select * from user where concat (username, ',', password) like '% admin000000 % ';

If the Delimiter is a space or other symbols, modify 'and' to 'delimi.

Summary:

Select * from table name where concat (Field 1, 'delimiter', Field 2, 'delimiter ',... field n) like '% keyword 1%' and concat (Field 1, 'delimiter', Field 2, 'delimiter ',... field n) like '% keyword 2% '......;

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.