我正在尝试接受索引列表,然后使用它们将varchar设置为某个字符串以过滤我的数据库。问题是我尝试使用Like命令:
Declare StateList varchar(150);
Declare States varchar(150);
set @StateList = State;
set @States = '';
if (@StateList Like '%1%')then set @States := Concat(@States, '1,2');
elseif(@StateList Like '%2%')then set @States := Concat(@States,'3');
elseif(@StateList Like '%3%')then set @States := Concat(@States, '4,5');
end if;
select @StateList,@States;
当我输入 123
时,如果我希望它返回1到5,则只返回3.
然后我试了
Declare StateList varchar(150);
Declare States varchar(150);
set @StateList = State;
set @States = '';
if CONTAINS(@StateList, '1')then set @States := Concat(@States, '1,2,');
elseif CONTAINS(@StateList ,'2')then set @States := Concat(@States,'3');
elseif CONTAINS(@StateList,'3')then set @States := Concat(@States, '4,5');
end if;
select @StateList,@States;
哪个没有返回,我感到很困惑,因为我认为第一个解决方案会有效吗?
任何建议都会很棒! 提前致谢
答案 0 :(得分:1)
您应该使用单独的IF's
,if,elseif
表示只有一个可以为真。尝试这样的事情:
if (@StateList Like '%1%')then set @States := Concat(@States, '1,2');
end if;
if(@StateList Like '%2%')then set @States := Concat(@States,'3');
end if;
if(@StateList Like '%3%')then set @States := Concat(@States, '4,5');
end if;
答案 1 :(得分:0)
我同意@sagi认为IF应该分开,但是我想知道结果是' 3'而不是' 1,2'。也许你可以尝试这个功能http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_instr 所以代码将是:
if (INSTR(@StateList, '1') > 0)then set @States := Concat(@States, '1,2');
end if;
...