i\'m trying to write a mySQL tigger but i can\'t put my code in phpMyAdmin MySQL without getting a syntax error. can someone help me please?
Tables :
There are many errors to correct.
inserted
contain only one row? Otherwise you need a where
clause or limit
.You better work more to learn.
Please refer to Trigger Syntax and Examples for better understanding.
Change your code as follows and it may work if all is well on your database objects.
drop trigger if exists after_jeu_insert;
delimiter //
CREATE TRIGGER after_jeu_insert after insert ON jeu for each row
BEGIN
DECLARE _game_id int;
DECLARE _old_turn int;
DECLARE _new_turn int;
-- following line may not require limit clause
-- if used proper where condition.
SELECT idpartie into _game_id FROM INSERTED limit 1;
SELECT tour into _old_turn FROM partie WHERE idpartie = _game_id;
IF _old_turn IS NULL then
SET _new_turn = 1;
ELSIF _old_turn = 1 then
SET _new_turn = 2;
ELSE
SET _new_turn = 1;
END IF;
UPDATE partie
SET tour = _new_turn
, derniercoup = NOW()
WHERE idpartie = _game_id;
END;
//
delimiter;