phpmyadmin mysql 触发语法错误

2022-01-05 00:00:00 mysql phpmyadmin triggers

我正在尝试编写一个 mySQL tigger,但我无法将我的代码放入 phpMyAdmin MySQL 中而不会出现语法错误.有人可以帮我吗?

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?

表格:

我的触发器:

CREATE TRIGGER after_jeu_insert ON jeu
   AFTER INSERT
   AS
    BEGIN
        DECLARE @gameid int, @oldTurn int, @newTurn int
        SELECT @gameid=idpartie FROM INSERTED
        SELECT @oldturn=tour FROM partie WHERE idpartie=@gameid
        IF(@oldTurn IS NULL)
        {
            SET @newTurn = 1
        }
        ELSE
        {
            IF(@oldTurn==1)
                SET @newTurn = 2
            ELSE
                SET @newTurn = 1
        }
        UPDATE partie SET tour = @newTurn, derniercoup = NOW() WHERE idpartie = @gameid
    END

我没有发现错误,如果有人可以帮助我,那就太好了.谢谢

I don't find the error, if someone could help me it would be very nice. Thanks

推荐答案

有很多错误需要更正.

  1. 没有结束的语句.
  2. 没有遵循语法顺序.
  3. 变量声明不正确.
  4. 从列中选择变量不正确.
  5. 不支持基于大括号的时钟.
  6. inserted 是否只包含一行?否则你需要一个 where 子句或 limit.
  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;

相关文章