MySQL Trigger code never called

52 Views Asked by At

I have a MySQL table like this

 ID    USER_ID   FIELD_ID    VALUE
 1        1         1          0
 2        1         2          2
 3        1         3          0<----- USER ID 0 
 .        .         .          .
 .        .         .          .
 .        .         .          .
 40       20        1          6
 41       20        2          3
 42       20        3          1<------ USER ID 1 
 .        .        .          .
 .        .         .          .
 .        .         .          .
 73       34        1          9
 74       34        2          6
 75       34        3          20<----- user ID 20
 .        .         .          .
 .        .         .          .
 .        .         .          .

field_id #3 above contains either "0" or the ID of another user.

I'm trying to write a MYSQL trigger that runs BEFORE UPDATE so that if user "A" is writing the ID of user B into field_id #3, and if user "B" field_id #3 is not "0" then write "0" in to user "A" field_id #3.

so the pseudo code for the trigger is basically:

  if (B["field_id3"]!="0") THEN
      A["field_id3"]="0";

The MYSQL trigger code is below, but it doesnt do anything, there are no syntax errors, but it doesnt work, anybody can help?

 BEGIN
   IF (NEW.FIELD_ID='3') THEN

     IF (EXISTS(SELECT USER_ID,FIELD_ID,VALUE FROM table WHERE USER_ID=NEW.VALUE)) THEN 

          IF (FIELD_ID='3' AND VALUE!='0') THEN
             SET NEW.VALUE='0'; # <<---NEVER GETS CALLED
          END IF;

     END IF;

  END IF;
 END
0

There are 0 best solutions below