2016-05-08 44 views
1

我需要比较存储过程中逗号分隔值的字符串。在Mysql中比较逗号分隔的字符串

输入随机串是= "'aa','ab','ac'"

我需要像

我需要类似下面的一个没有使用动态SQL的所得效果。

SELECT * FROM table1 
WHERE table1.field1 LIKE %aa% OR 
table1.field1 LIKE %ab% OR 
table1.field1 LIKE %ac% 

输入字符串是可变的,而不是固定的

回答

0

您可以通过这种方式方法,我不能用FIND_IN_SET:

SELECT 
    * 
FROM table1 
WHERE 
    table1.field1 REGEXP 
    REPLACE (REPLACE ("'aa','ab','ac'",'\'',''),',','|'); 

一般:

SELECT 
    * 
FROM table1 
WHERE 
    table1.field1 REGEXP 
    REPLACE (REPLACE (PUT_YOUR_INPUT_STRING_HERE,'\'',''),',','|') 

为了要理解那里发生了什么,下面的查询可能会导致:

SELECT 
REPLACE("'aa','ab','ac'",'\'','') afterFirstReplace, 
REPLACE(REPLACE("'aa','ab','ac'",'\'',''),',','|') afterSecondReplace; 

afterFirstReplace    afterSecondReplace 
aa,ab,ac       aa|ab|ac