Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9188355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:59:09+00:00 2026-06-17T19:59:09+00:00

I am trying to create a trigger in MySQL that is fired after a

  • 0

I am trying to create a trigger in MySQL that is fired after a table gets updated.
(I have had a little problem, that there will be a bunch of rows inserted before my code should be called. The first if statement is checking on that.)
So I get the error #1422 - Explicit or implicit commit is not allowed in stored function or trigger.

That’s my code:

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;

I hope you can help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:59:10+00:00Added an answer on June 17, 2026 at 7:59 pm

    The problem is your TRUNCATE statement. That statement cannot be executed from a trigger, because that statement performs an implicit commit. (There may be other reasons as well, but that one is sufficient.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create an Oracle trigger that runs after a table is updated in
I´m trying to create a trigger on MySQL but I´m having a syntax problem,
I'm trying to create a trigger that will do the following. After insert on
I'm trying to create a trigger but the error shown after typed DECLARE. mysql>
I am trying to create a Trigger for a table that UDATES. I thought
I'm trying to create a trigger that monitor changes on a table and then
I am trying to create a Trigger, that will update the History Table with
I am trying to create a trigger to update another table with a condition
I am trying to update a table according to this trigger: CREATE TRIGGER alert
I'm trying to create a basic database trigger that conditionally deletes rows from database1.table1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.