mysql fuzzy query 1, 11, 111 similar strings spliced with commas (other symbols)

mysql fuzzy query 1, 11, 111 similar strings spliced ​​with commas (other symbols)

 

When mysql performs fuzzy query, it is basically LIKE "%sss%". Sometimes this query is accurate, but in some cases, this query will have a big problem.

Take a look at the table below

If we want to query the data whose field test contains 1, we generally query in the usual way, as follows:

SELECT * FROM c_test WHERE test LIKE "%1%"

But the query results are not satisfactory, as follows:

 

Not only the ones containing 1, but also the ones containing 11 and 110 are queried. This is not the result we want, because we only want the data with id 1

At this point, there is a big problem with sql. We can make the following modifications, add commas to both ends of the test field value, and then LIKE query, there will be no such problem.

sql is as follows:

SELECT * FROM c_test WHERE CONCAT(",",test,",") LIKE "%,1,%"

The query results at this time are as follows:

This is the result we want.

There is another way, use the mysql function FIND_IN_SET(str, strlist)

 sql is as follows:

SELECT * FROM c_test WHERE FIND_IN_SET("1",test)

 

 But this method is only applicable to strings separated by commas, so be careful when using it

Split the sky and create the land, open up your own world! ! ! share with you
{{o.name}}
{{m.name}}

Guess you like

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