문제

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