MySQL multi-field multi-keyword fuzzy query

Suppose there are two such data:

(Table named user)

1) username=admin,password=000000

2) username=admin,password=123456

The effect we want to achieve is a query can enter multiple keywords, separated by commas between multiple keywords.

Using the above table illustrates: single keyword input "admin" may be isolated these two data input "admin, 000000" isolated only the first data, sql statement is implemented:

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

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

Concat role is to connect the string, but there is such a question: If you enter a single keyword results "admin000000" can also be found in the first data, which is obviously not what we want, the solution is: the use of a comma-separated more than keywords, description comma will never become part of the key, so we put each field separated by commas to resolve this problem in the connection string, the following sql statement does not query to the first data:

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

If the separator is a space or other symbols, modified ',' as 'separator' can.

to sum up:

select * from table where concat (field 1, 'separator', 2 field, 'separator', ... the field n) like '% Keyword 1%' and concat (field 1, 'separator' field 2, 'separator', ... the field n) like '% 2% keyword' ......;

Guess you like

Origin www.cnblogs.com/snake107/p/11922657.html