Mysql筛选器表,多个值之间用分号分隔

时间:2018-11-20 12:21:37

标签: mysql

我正在尝试过滤我的表,其中where子句具有多个用分号分隔的值。我正在尝试使用set中的find,但是它没有任何输出。

tblAddress:

Address       Mat           month
tblA           X            01
tblA           Y            01
tblB           Z            01
tblB           1            01
tblC           Y            01
tblC           J            01
tblD           M            01
tblD           S            01
tblA           X            02
tblA           Y            02
tblB           Z            02
tblB           1            02
tblC           Y            02
tblC           J            02
tblD           M            02
tblD           S            02

我正在尝试按月份和地址过滤

SELECT * 
FROM tblAddress 
WHERE month  BETWEEN "01" AND "02"
    AND month  BETWEEN "01" AND "02"
    AND FIND_IN_SET(Address, REPLACE("tblA;tblC", ';', ',') )

当前输出无结果显示

但我想要的输出

tblAddress

Address       Mat           Month
tblA           X            01
tblA           Y            01
tblC           Y            01
tblC           J            01
tblA           X            02
tblA           Y            02
tblC           Y            02
tblC           J            02

我想知道它是否可以使用findinset或在哪里?非常感谢您的任何建议

CREATE TABLE tblAddress (Address VARCHAR(20), Mat VARCHAR(20),month VARCHAR(20));

2 个答案:

答案 0 :(得分:0)

尝试一下

SELECT * 
FROM tblAddress 
WHERE Month IN (01,02)
    AND Address IN ('tblA','tblc')
ORDER BY Address;

答案 1 :(得分:0)

这不是一个语句,但这是一个纯MySQL解决方案:

SET @csv_list = REPLACE("tblA;tblC", ';', ',');
SET @expression = CONCAT('SELECT * FROM tblAddress WHERE Month IN (01,02) AND s.Mall IN (', @csv_list, ') ORDER BY Address');
PREPARE myquery FROM @expression;
EXECUTE myquery

另一种选择是确保变量使用分号进行前缀和后缀,然后使用以下命令:

SELECT * 
FROM tblAddress 
WHERE Month IN (01,02) 
    AND LOCATE(CONCAT(';', s.Mall, ';'), ";tblA;tblC;" )
ORDER BY Address