MySQL if语句忽略第一个条件

时间:2019-01-05 10:45:21

标签: mysql sql mysql-workbench

我在mysql过程中有一个if语句,如下所示:

if myQuery like '%فواید%' or myQuery like '%مضرات%' or myQuery like 
'%خواص%' or myQuery like '%عوارض%' then
set  EHSfour= b'1';
end if;

令我惊讶的是,我发现上面的if语句忽略了myQuery,例如'%فواید%'。它会检查其他条件,并在EHSfour = 1时设置myQuery is like '%مضرات%' or '%خواص%' or '%عوارض%',但在myQuerylike '%فواید%'时设置if EHSfour = b'1' then set myEHSkind = 4;

为什么地狱的mysql会那样工作?在什么情况下mysql可能会那样作?

mysql版本是5.7,我正在使用mysql workbench 6.3 CE。

更新: 在我的代码中,我有:

update new_tbl_health set EHS_kind = myEHSkind where ID = myID;

然后:

CREATE DEFINER=`root`@`localhost` PROCEDURE `fillEHSKind`()
BEGIN
declare myEHSkind int default 0;
declare EHSone,EHStwo,EHSthree,EHSfour bit;
declare myID int(11);
declare myQuery varchar(500);
declare v_finished integer default 0;
declare myCursor cursor for select ID,Query from new_tbl_health;
declare continue handler for not found set v_finished = 1;
open myCursor;
myLoop: loop
fetch myCursor into myID,myQuery;
if v_finished = 1 then
leave myLoop;
end if;

set EHSone = b'0';
set EHStwo = b'0';
set EHSthree = b'0';
set EHSfour = b'0';
set myEHSkind = 0;

if myQuery like '%بیمارستان%' or myQuery like '%دکتر%' or myQuery like 
'%پزشک%' then
set  EHSone= b'1';
end if;

if myQuery like '%بیماری%' or myQuery like '%مریضی%'
set EHStwo = b'1';
end if;

if myQuery like '%علایم%' or myQuery like '%نشانه%' or myQuery like 
'%علت%' or myQuery like '%خارش%'  then
set EHSthree = b'1';
end if;

if myQuery like '%فواید%' or myQuery like '%مضرات%' or myQuery like 
'%خواص%' 
or myQuery like '%عوارض%' then
set  EHSfour= b'1';
end if;

if EHSone = b'1' and EHStwo = b'1' and EHSthree=b'1' then
set myEHSkind = 8;
elseif EHSone = b'1' and EHStwo = b'1' then
set myEHSkind = 5;
elseif EHSone = b'1' and EHSthree = b'1' then
set myEHSkind = 6;
elseif EHStwo = b'1' and EHSthree = b'1' then
set myEHSkind = 7;
elseif EHSone = b'1' then
set myEHSkind = 1;
elseif EHStwo = b'1' then
set myEHSkind = 2;
elseif EHSthree = b'1' then
set myEHSkind = 3;
elseif EHSfour = b'1' then
set myEHSkind = 4;
end if;

update new_tbl_health set EHS_kind = myEHSkind where ID = myID;
end loop myLoop;
END

这是我的整个过程:

{{1}}

1 个答案:

答案 0 :(得分:0)

我没有使用if,但是很可能您的if语句仅影响符合条件的第一行。

如果要更新所有符合条件的行,请尝试以下操作:

update #tableName set EHSfour= b'1' where (myQuery like '%فواید%' or myQuery like '%مضرات%' or myQuery like '%خواص%' or myQuery like '%عوارض%');