且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL查询:将逗号分隔的值与包含逗号分隔的字符串的列匹配

更新时间:2021-08-23 00:52:35

可以使用regexp来实现,如@ 1000111所述,但是使用更复杂的regexp.看看这个,例如:

It can be done with regexp as @1000111 said, but with more complicated regexp. Look at this, for example:

(^|,)(13|15)(,|$)

这将不匹配135中的13,或13中的1,依此类推.例如,对于数字13,它将匹配下一个字符串:

This will not match 13 from 135, or 1 from 13 and so on. For example, for number 13 this will match next strings:

1,13,2
13,1,2
1,13
13,2
13

但不会与这些匹配

1,135,2
131,2
1,113

这是查询:

SET @search = '13,15';

SELECT *
FROM test
WHERE interests REGEXP CONCAT('(^|,)(', REPLACE(@search, ',', '|'), ')(,|$)')