phpmyadmin mysql trigger syntax error

后端 未结 1 989
情话喂你
情话喂你 2021-01-22 14:02

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 :

1条回答
  •  醉梦人生
    2021-01-22 14:59

    There are many errors to correct.

    1. No end of statements.
    2. No order of syntax followed.
    3. Declaration of variables is not correct.
    4. Selection in to variables from a column is not correct.
    5. Brace based clocks is not supported.
    6. Does table inserted contain only one row? Otherwise you need a where clause or limit.
    7. etc.

    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;
    

    0 讨论(0)
提交回复
热议问题