MySQL Trigger – delete after update

MySQLsqlsql-deletesql-updatetriggers

For test case in one project I must delete record if one of fields meets one of conditions.
Easiest seems be simple trigger:

delimiter $$
drop trigger w_leb_go $$

create trigger w_leb_go after update on test
for each row
begin
set @stat=NEW.res;
set @id=NEW.id;
IF @stat=3 THEN
  delete from test where id=@id;
END IF;
end$$

delimiter ;

But as I suspect got error:

update test set res=3 where id=1;
ERROR 1442 (HY000): Can't update table 'test' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

How else can be done only by having access to the database? I mean that I should not change the tested application.

Best Answer

What you may need to do is use the 'BEFORE INSERT' of the trigger to check if the record being added is not valid.

This SO question provides some detail on how to dipose of the invalid record: Prevent Insert

The alternate is to modify your program to insert a record into another table which has a trigger to purge the desired table.