2013-02-19 62 views

回答

1

为什么使用正则表达式?

SELECT .. 
WHERE LEFT(yourfield, 1) <> '-' 
    AND (
    (yourfield = 2) OR 
    (yourfield LIKE '2,%') OR 
    (yourfield LIKE '%,2,%') OR 
    (yourfield LIKE '%,2') 
) 

,然后当你试图找出为什么哪里是那么复杂,你应该去阅读上涨约database normalization

1

我会倾斜like做到这一点:

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 '-%' 

要获取不带有负号开始,constains列中的“2”:

where col regexp '^[^-]*2*' 

如果逗号是很重要的,下面应该工作:

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

谢谢男人..我知道如何做到这一点,但我需要正则表达式,只有一个 – 2013-02-19 15:56:03

0

的最佳解决方案和最简单的,我可以找到:

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