I have a hypothetical table, fruit. It looks like this:

Product | Value

Apple     1
Orange    2

I am trying to create a trigger that will execute before update that will add 2 to the value only if the new value was for the apple. What would this trigger look like? Example someone executed

UPDATE fruit SET value=2 WHERE product='apple';

The new value for apple would be 4.

I tried the trigger below but of course that updates any value with 2

BEGIN 
  SET value = new.value+2
END
有帮助吗?

解决方案

Use code something like this in your trigger:

if new.product = 'Apple' then
    set new.value = new.value + 2;
end if;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top