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 6795035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:17:39+00:00 2026-05-26T18:17:39+00:00

I have a table (ft_ttd) and want to sort it descending (num) and insert

  • 0

I have a table (ft_ttd) and want to sort it descending (num) and insert rating numbers into rating column.

Initial Table http://dl.dropbox.com/u/3922390/2.png

Something like that:

Result Table http://dl.dropbox.com/u/3922390/1.png

I’ve created a procedure.

CREATE PROCEDURE proc_ft_ttd_sort  
BEGIN   

CREATE TEMPORARY TABLE ft_ttd_sort
(id int (2),  
num int (3),  
rating int (2) AUTO_INCREMENT PRIMARY KEY);    

INSERT INTO ft_ttd_sort (id, num)   SELECT id, num FROM ft_ttd ORDER BY num DESC;    
TRUNCATE TABLE ft_ttd;   
INSERT INTO ft_ttd SELECT * FROM ft_ttd_sort;  
DROP TABLE ft_ttd_sort;   
END;

When I call it – it works great.

CALL proc_ft_ttd_sort;

After that I’ve created trigger calling this procedure.

CREATE TRIGGER au_ft_ttd_fer AFTER UPDATE ON ft_ttd FOR EACH ROW 
BEGIN
CALL proc_ft_ttd_sort(); 
END;

Now every time when I update ft_ttd table I’ve got a error.

UPDATE ft_ttd SET num = 9 WHERE id = 3;
ERROR 1422 (HY000): Explicit or implicit commit is not allowed in stored function ortrigger.

Any ideas how to make it work? Maybe this process can be optimized?
Thank you!

  • 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-05-26T18:17:40+00:00Added an answer on May 26, 2026 at 6:17 pm

    Triggers can’t do it

    DDL aside, your trigger-based approach has a few difficulties. First, you want to modify the very table that’s been updated, and that’s not permitted in MySQL 5.

    Second, you really want a statement-level trigger rather than FOR EACH ROW — no need to re-rank the whole table for every affected row — but that’s not supported in MySQL 5.

    Dynamically compute "rating"

    So … is it enough to just compute rating dynamically using a MySQL ROW_NUMBER() workaround?

    -- ALTER TABLE ft_ttd DROP COLUMN rating; -- if you like
    
        SELECT id,
               num,
               @i := @i + 1 AS rating
          FROM ft_ttd
    CROSS JOIN (SELECT @i := 0 AS zero) d
      ORDER BY num DESC;
    

    Unfortunately, you cannot wrap that SELECT in a VIEW (since a view’s “SELECT statement cannot refer to system or user variables”). However, you could hide that in a selectable stored procedure:

     CREATE PROCEDURE sp_ranked_ft_ttd () BEGIN
         SELECT id, num, @i := @i + 1 AS rating
           FROM ft_ttd CROSS JOIN (SELECT @i := 0 AS zero) d
       ORDER BY num DESC
     END
    

    Or UPDATE if you must

    As a kluge, if you must store rating in the table rather than compute it, you can run this UPDATE as needed:

        UPDATE t
    CROSS JOIN (    SELECT id, @i := @i + 1 AS new_rating
                      FROM ft_ttd
                CROSS JOIN (SELECT @i := 0 AS zero) d
                  ORDER BY num DESC
               ) ranked
            ON ft_ttd.id = ranked.id SET ft_ttd.rating = ranked.new_rating;
    

    Now instruct your client code to ignore rows where rating IS NULL — those haven’t been ranked yet. Better, create a VIEW that does that for you.

    Kluging further, you can likely regularly UPDATE via CREATE EVENT.

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

Sidebar

Related Questions

i have table structure with 3 colums (column1, column2, column3) and i want to
I have table with 3 columns A B C. I want to select *
I have table named A with column B defined int not null Primary Key
I have a large dataframe and want to tabulate all variable-paris. table() and xtabs()
I have table inside a div tab. The table has 40 rows in it
I have table with 50 entries (users with such details like Name Surname Location
I have table rows of data in html being filled from a CGI application.
I have table with some fields that the value will be 1 0. This
I have table with a unique auto-incremental primary key. Over time, entries may be
i have table unser it i have one td with elemets inside the menu

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.