0

Is it possible to create a trigger in mysql for this:

I have a table like this

Column1_id Column2_val
 1            2
 2            2

Second table

Column1_id1 Column2_id2
    1           2

So pretty much on update or insert if twos rows have the same value for Column2_val I want to insert the ids of that into the second table. any help would be greatly appriciated.

I wrote this but it doesn't seem to be working am i doing something wrong?

BEGIN
    set @user_id_update =  (SELECT Column1_id from users where in_play = 2 LIMIT 1);
    INSERT INTO player_pairs(PLAYER1_ID, PLAYER2_ID, GAME_ID) 
    VALUES (new.Column1_id, @user_id_update, 2); 
    UPDATE users  SET in_play = 1 where Column1_id in(@user_id_update, new.user_id);
END 
4

2 に答える 2

0

このようなことを試してください

DELIMITER $$

    CREATE TRIGGER test AFTER INSERT, UPDATE ON `table1`
    FOR EACH ROW BEGIN
      SET x = (SELECT count(*) FROM `table1` WHERE Column2_val=NEW.Column2_val)
      IF (x > 1) THEN
            INSERT INTO `table2` VALUES(NEW.Column1_id, NEW.Column2_val)
      END IF;
    END$$

DELIMITER ;
于 2012-12-16T06:57:28.157 に答える