mysql regexp有一个数字,但不以负数开头

mysql regexp有一个数字,但不以负数开头,mysql,regex,Mysql,Regex,我有一些行的值像-4,3,2,1和3,2,1或3或2 我需要选择任何具有值2的行,包括具有值3,2,1的行,但不是一开始就具有值的行 谢谢大家为什么是正则表达式 SELECT .. WHERE LEFT(yourfield, 1) <> '-' AND ( (yourfield = 2) OR (yourfield LIKE '2,%') OR (yourfield LIKE '%,2,%') OR (yourfield LIKE '%,2')

我有一些行的值像
-4,3,2,1
3,2,1
3
2

我需要选择任何具有值
2
的行,包括具有值
3,2,1
的行,但不是一开始就具有值的行

谢谢大家

为什么是正则表达式

SELECT ..
WHERE LEFT(yourfield, 1) <> '-'
  AND (
    (yourfield = 2) OR
    (yourfield LIKE '2,%') OR
    (yourfield LIKE '%,2,%') OR
    (yourfield LIKE '%,2')
 )
选择。。
左上角(您的字段,1)“-”
及(
(yourfield=2)或
(您的字段类似于“2,%”)或
(您的字段,如“%”、2、%”)或
(您的字段类似于“%,2”)
)

然后,当你试图弄清楚为什么WHERE这么复杂时,你应该去读一下关于的内容,我倾向于用
这样的

select *
from t
where concat(',', col, ',') like '%,2,%' and
      col not like '-%'
如果要查找“2”,但未用逗号分隔,则:

select *
from t
where instr(col, '2') > 0 and col not like '-%'
要获取不以减号开头且包含“2”的列,请执行以下操作:

如果逗号很重要,则应采用以下方法:

where concat(',', col, ',') regexp ',^[^-]*,2,*'

我能找到的最佳解决方案和最简单的解决方案:

where sponsored regexp '^[^-]*[1-2]'

谢谢你,我知道如何使用like,但是我需要使用regexp,而且只有一个
where sponsored regexp '^[^-]*[1-2]'