多值分号mysql筛选表

fcwjkofz  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(280)

我试图过滤我的表,使where子句有多个用分号分隔的值。我试图使用find-in-set,但它不提供任何输出。
t装饰:

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或where?我真的很感激你的建议谢谢

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

dbf7pr2w1#

这不是一个单独的语句,而是一个纯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
55ooxyrt

55ooxyrt2#

试试这个

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

相关问题