80、存储过程中使用like模糊一个数组
1、使用group_concat把数组变成字符串
2、使用replace替换连接符
3、使用concat连接拼接脚本
需要模糊的数组
DECLARE ces_name VARCHAR(255) DEFAULT '';
select GROUP_CONCAT(name) into ces_name from (
select sdi.name from sys_dict sd
inner join sys_dict_item sdi
on sd.id = sdi.DICT_ID
where sd.code = 'jzf_qs_qlmc'
and sd.IS_DEL = '0'
and sdi.IS_DEL = '0'
)aa;
假如这个数组数据是:'apple,orange,xxx'
假如模糊比配的字段是name
REPLACE('apple,orange,xxx', ',', '%" OR name LIKE "%')
得到如下数据
apple%" OR name LIKE "%orange%" OR name LIKE "%xxx
缺少头和尾巴,需要再次填充
CONCAT('(name LIKE "%',REPLACE('apple,orange,xxx', ',', '%" OR name LIKE "%'), ',', '%" OR name LIKE "%'), '%")');
得到如下数据
(name LIKE "%apple%" OR name LIKE "%orange%" OR name LIKE "%xxx%")
已经成功得到想要的条件脚本