我有MySQL存储的proc,我在其中输入带有多个字段的JSON字符串,这些字段是数组列表,具体取决于在HTML SELECT表单中选择的内容:
{
"date1": "2020-01-26",
"date2": "2020-01-27",
"param1": ["Alpha","Bravo"],
"param2": [],
"param3": ["A","B","C"]
"param4": []
"param5": ["1","2","3"]
}
现在要根据所选参数建立查询,以在我的表格中搜索哪里。如果数组列表为空,则不要通过此参数进行搜索并将所有参数都接受。
if (json_length(param1)>0) then
set @and_param1=concat('and `param1` in (replace(replace(param1, \'[\', \'\'), \']\', \'\'))');
end if;
if (json_length(param2)>0) then
set @and_param2=concat('and `param2` in (replace(replace(param2, \'[\', \'\'), \']\', \'\'))');
end if;
if (json_length(param3)>0) then
set @and_param3=concat('and `param3` in (replace(replace(param3, \'[\', \'\'), \']\', \'\'))');
end if;
if (json_length(param4)>0) then
set @and_param4=concat('and `param4` in (replace(replace(param4, \'[\', \'\'), \']\', \'\'))');
end if;
if (json_length(param5)>0) then
set @and_param5=concat('and `param5` in (replace(replace(param5, \'[\', \'\'), \']\', \'\'))');
end if;
select * from table where `date` between date1 and date2 @and_param1 @and_param2 @and_param3 @and_param4 @and_param5
但是无法找出有效的查询...
尝试在AND运算符之前添加空格字符(对于所有var而言)
set @and_param1=concat(' and `param1` in (replace(replace(param1, \'[\', \'\'), \']\', \'\'))');