Mysql comma ',' how to query the matching data contained in the spliced string?

Up data:

 You can see the way of comma splicing in the way sn is stored.

So how to do the matching search of the sn field?

① like (not considered)

First of all, like is not acceptable, unless you can ensure that the data in your comma splicing does not contain repeated values, such as 1 and 11. If you use like to find 1, you will directly find out the data of 1 and 11.

So like is not considered.

②regexp regular matching 

This is OK, but pay attention to the way of writing, we are splicing with commas, so the regex must be written correctly.

Let's look at the correct usage example first:

Single value: REGEXP '(^|,)(value to be found)(,|$)'

Multiple values: REGEXP '(^|,)(first value|second value)(,|$)'

Multiple middles need to be separated by |


select * from product_extra where sn REGEXP '(^|,)(1)(,|$)';

 

 

 Multiple matches:


 

③find in set single search

FIND_IN_SET(str, strlist) : str is the string to be queried , strlist is the field to be queried, the parameters are separated by ",", the form is like (1,2,6,8,10,22); the function of this function is to query the field Whether (strlist) contains the result of (str), the return result is null or record.

   SELECT  *  FROM  product_extra WHERE FIND_IN_SET('1',sn);

 

But  find in set   is a single match, if you want to match multiple at the same time, you need to use or to combine.

  SELECT * FROM  product_extra 
  
  WHERE FIND_IN_SET('11',sn) OR FIND_IN_SET('3',sn);

 

Well, that's all for this article.

Guess you like

Origin blog.csdn.net/qq_35387940/article/details/131421252
Recommended