您可以设置 NEW。前触发器中的值,并且因为 mysql 复制 OLD。新的值。在它获得 NEW 之前的值。来自更新的值然后测试新的。值是安全的
drop table if exists t;
create table t
( id int, province varchar(10), sample_result int, image varchar(10), status varchar(10));
insert into t values
( 1 , 'prov_1' , 3 , 'url' , 'Complete'),
( 2 , 'prov_2' , 12 , NULL , null ),
( 3 , 'prov_3' , 45 , NULL , null ),
( 4 , 'prov_4' , null , 'url' , null );
drop trigger if exists t;
delimiter $$
create trigger t before update on t
for each row
begin
if new.sample_result is not null and new.image is not null then
set new.status = 'Complete';
end if ;
end $$
delimiter ;
update t set sample_result = 2 where id = 2;
update t set sample_result = 1 where id = 4;
update t set image = 'url' where id = 3;
select * from t;
+------+----------+---------------+-------+----------+
| id | province | sample_result | image | status |
+------+----------+---------------+-------+----------+
| 1 | prov_1 | 3 | url | Complete |
| 2 | prov_2 | 2 | NULL | NULL |
| 3 | prov_3 | 45 | url | Complete |
| 4 | prov_4 | 1 | url | Complete |
+------+----------+---------------+-------+----------+