0

我正在尝试在 MySQL 中创建一个在表更新后触发的触发器。(我遇到了一个小问题,在调用我的代码之前会插入一堆行。第一个 if 语句正在检查它。)所以我得到了错误#1422 - Explicit or implicit commit is not allowed in stored function or trigger.

那是我的代码:

DELIMITER //

CREATE TRIGGER
    `Update_Player_Rankings`
AFTER INSERT ON
    `sg_playerstats`
FOR EACH ROW

BEGIN
    SET
        @test1 = 0,
        @test2 = 0;

    SELECT
        `gameno`
    INTO
        @test1
    FROM
        `sg_gamestats`
    ORDER BY
        `gameno` DESC
    LIMIT
        0,
        1;

    SELECT
        COUNT(*)
    INTO
        @test2
    FROM
        `sg_playerstats`
    WHERE
        `gameno` = @test1
    GROUP BY
        `gameno`
    LIMIT
        0,
        1;

    IF
        @test1 = @test2
    THEN

        SET
            @var1 = 1,
            @var2 = 0;

        SELECT
            `id`
        INTO
            @var1
        FROM
            `sg_playerstats`
        ORDER BY
            `id` DESC
        LIMIT
            0,
            1;

        SELECT
            `Last Updated`
        INTO
            @var2
        FROM
            `sg_playerranking`
        LIMIT
            0,
            1;

        IF
            @var1 > @var2
        THEN

            TRUNCATE
                `sg_playerranking`;

            SET
                @Rank := 0;

            INSERT INTO
                `sg_playerranking`
            SELECT
                @Rank := @Rank + 1 AS `Rank`,
                t.*,
                @var1 AS `Last Updated`
            FROM
                (
                    SELECT
                        `player` AS `Player`,
                        SUM(1) AS `Games`,
                        SUM(`points`) AS `Points`,
                        SUM(`points`) / SUM(1) AS `Points per Game`,
                        SUM(IF(`position` = 1, 1, 0)) AS `Wins`,
                        SUM(IF(`position` = 1, 1, 0)) / SUM(1) AS` Wins per Game`,
                        SUM(IF(`position` = 1, 0, 1)) AS `Loses`,
                        SUM(IF(`position` = 1, 0, 1)) / SUM(1) AS `Loses per Game`,
                        SUM(`kills`) AS `Kills`,
                        SUM(`kills`) / SUM(1) AS `Kills per Game`,
                        SUM(`death`) AS `Deaths`,
                        SUM(`death`) / SUM(1) AS `Deaths per Game`,
                        SEC_TO_TIME(SUM(`time`) / 1000) AS `Time played`,
                        ROUND(sqrt(SUM(`points`)) + 10 * SUM(`points`) / SUM(1) + 10 * sqrt(SUM(IF(`position` = 1, 1, 0))) + 100 * SUM(IF(`position` = 1, 1, 0)) / SUM(1) - 5 * sqrt(SUM(IF(`position` = 1, 0, 1))) - 50 * SUM(IF(`position` = 1, 0, 1)) / SUM(1) + 7.5 * sqrt(SUM(`kills`)) + 75 * SUM(`kills`) / SUM(1) - 3.75 * sqrt(SUM(`death`)) - 37.5 * SUM(`death`) / SUM(1), 2) AS `Score`
                    FROM
                        `sg_playerstats`
                    GROUP BY
                        `player`
                    ORDER BY
                        `Score` DESC,
                        `Points per Game` DESC,
                        `Wins per Game` DESC,
                        `Loses per Game` ASC,
                        `Kills per Game` DESC,
                        `Deaths per Game` ASC,
                        `Points` DESC,
                        `Wins` DESC,
                        `Loses` ASC,
                        `Kills` DESC,
                        `Deaths` ASC,
                        `Games` DESC,
                        `Time played` DESC,
                        `Player` DESC
                ) t;
        END IF;
    END IF;
END;

//

DELIMITER;

我希望你能帮忙!

4

1 回答 1

2

问题是您的 TRUNCATE 语句。该语句不能从触发器执行,因为该语句执行隐式提交。(可能还有其他原因,但一个就足够了。)

于 2013-01-24T00:07:49.917 回答