MySQL触发器与多个IF语句

问题描述 投票:0回答:1

我是新来的,似乎找不到与我类似的情况,我相信答案很简单。

我有两列,一列是预定义的 licenseplate("licenseplate"),一列是用户输入的 licenseplate("enterlicenseplate")。

我做了第三列,这将是一种错误检查列,其值将由一个触发器输入。基本上,如果输入的车牌和预定义的车牌一样,就把第三列设置为0,如果输入的车牌是空的(还没有输入任何东西),就把它设置为1,如果两者的值不匹配,就把它设置为2。但我一直收到脚本错误。不知道是我的语法问题还是我的方法不对。

我很感激任何帮助

 CREATE TRIGGER MatchPlateOnUPDATE 
 BEFORE UPDATE ON wp_wpdatable_3
 FOR EACH ROW 
 BEGIN
 IF (NEW.enterlicenseplate is NULL) THEN
 UPDATE wp_wpdatatable_3
    SET MatchingPlates = 0;

  ELSEIF (NEW.enterlicensplate = New.licenseplate) THEN
 UPDATE wp_Wpdatatable_3
    SET MatchingPlates = 1;
ELSE UPDATE wp_Wpdatatable_3
SET MatchingPlates = 2;
END
mysql sql if-statement sql-update database-trigger
1个回答
0
投票

你的问题与 if 声明是,你缺失关闭 end if;.

但还有更多:触发器不能对它所触发的表进行操作--即使它可以,那么你的查询基本上会更新表中的所有记录,因为你的 update的没有 where 子句。

在这里,你只需要修改列的值。matchingPlates 在即将插入的记录中。

delimiter //

create trigger matchplateonupdate 
before update on wp_wpdatable_3
for each row 
begin
    if new.enterlicenseplate is null then
        set new.matchingplates = 0;
    elseif new.enterlicensplate = new.licenseplate then
        set new.matchingplates = 1;
    else 
        set new.matchingplates = 2;
    end if;
end;
//

delimiter ;
© www.soinside.com 2019 - 2024. All rights reserved.