prod_catg LIKE '1,%'
OR prod_catg LIKE '%,1,%'
OR prod_catg LIKE '%,1'
无论如何,您最好通过添加类别表并在产品(主)表上引用它来重构架构。
编辑
另一种解决此问题的方法是使用REGEXP,这将导致更短的WHERE
子句(这是我用来测试的内容):
DECLARE @regexp VARCHAR(100);
SET @regexp = '^1,.*|.*,1$|.*,1,.*';
SELECT
'1,11,15,51,22,31' REGEXP @regexp AS test1,
'51,11,15,1,22,31' REGEXP @regexp AS test2,
'11,15,51,22,31,1' REGEXP @regexp AS test3,
'7,11,15,51,22,31' REGEXP @regexp AS test4,
'51,11,15,7,22,31' REGEXP @regexp AS test5,
'11,15,51,22,31,7' REGEXP @regexp AS test6;
这将匹配您的
prod_catg
与正则表达式
'^1,.*|.*,1$|.*,1,.*'
,如果匹配,则返回
1(TRUE)
,否则返回
0(FALSE)
。然后您的WHERE子句将如下所示:
WHERE prod_catg REGEXP '^1,.*|.*,1$|.*,1,.*'
正则表达式的解释:
^1,.* --matches 1 at the beginning of a string followed by a `,` and any other char
.*,1$ --matches 1 at the end of a string preceded by a `,` and any other char
.*,1,.* --matches 1 between two `,` which are sourrounded by any other chars
| --is the OR operator
我相信这个正则表达式可以更加简洁,但我不太擅长正则表达式。
显然,您可以在正则表达式中更改要查找的类别(尝试将上面的
1
替换为
7
)。