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

  • SEARCH
  • Home
  • 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 6083803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:26:45+00:00 2026-05-23T11:26:45+00:00

Hello StackOverflow community: The Situation I am building an update query that sets ranks

  • 0

Hello StackOverflow community:

The Situation

I am building an update query that sets ranks for all records in a table according to how each record compares to each other. Example of table:

id | budget | cost | rank | rank_score  
 1 |   500  |  20  |   ?  |     ?  
 2 |   400  |  40  |   ?  |     ?  
 3 |   300  |  40  |   ?  |     ?

So in this table, cost have the most weight in determining rank, followed by budget. Thus, record #2 will rank higher, while record #3 will be second and #1 will be last. As you can see, if two records have the same cost, then budget breaks the tie.

Now, In order to keep track of such ‘weight’ easily, I’m creating the rank_score column, which will hold the concatenation of cost and budget. Thus the rank_score for the table above would be:

id | budget | cost | rank | rank_score  
 1 |   500  |  20  |   ?  |   20500   
 2 |   400  |  40  |   ?  |   40400  
 3 |   300  |  40  |   ?  |   40300  

This rank_score can be filled like:

UPDATE table_name
SET rank_score = CONCAT(cost, budget);

The Problem

Everything alright so far. But now comes the problem. I need an integer-only rank column for several stuff like sorting, but above all for showing the user the rank of his record. Of course, this rank column will be equal to the descending order of the rank_scores. But I cannot find a way to calculate this rank column in a single update query without having to do subqueries, loops in php, etc.

What I Have Tried

So, at first I was trying to fetch the rank_score calculation like:

SELECT id,
       CONCAT(cost, budget) AS rank_score
FROM table_name ;

and then looping in php all those rank_scores, only to build a query that went like:

UPDATE table_name
SET rank_score = CASE id WHEN 1 THEN 20500 END,
    rank = CASE id WHEN 1 THEN 3 END
WHERE id IN (1) ;

… Of course, this sample update query is not complete, as it has more WHEN THEN END clauses for each record in the table. Needless to say, this is ugly, especially when you expect to be having thousands and thousands of records.

So, in conclusion, I already have a way for calculating rank_score, but I also want to calculate rank (= descending order of rank score) in the same query, or at least without doing that crazy php looping and CASE WHEN THEN END clauses.

Thank You for Reading and Thinking About This 😉

Clarifications

Clarifying what @SJuan76 said:
I cannot assign a rank via php since there are instances when the user will be shown a fixed quantity of records at a time (example, his user page: SELECT * WHERE user_id = 333, which could fetch 1, 3 or 8 records) and he needs to know what’s the rank for each record. Assigning a rank via php in that case doesn’t work because such rank will be relative to fetched records, not to all in table.

  • 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-23T11:26:45+00:00Added an answer on May 23, 2026 at 11:26 am

    First, I would change budget, cost and rank_score into integer or other numeric datatype and instead of

    UPDATE table_name
    SET rank_score = CONCAT(cost, budget) ;
    

    Then you would use:

    UPDATE table_name
    SET rank_score = cost * 1000 + budget * 1  ;
    

    It’s easier then as you won’t have to deal with string functions and to have something like:

    SELECT * 
    FROM table_name
    WHERE (conditions...)
    ORDER BY rank_score DESC
    

    (Parenthesis: having one parameter (1000) set so higher than the other (1) is equivalent to having an order of cost, budget. Try this to check:

    SELECT * 
    FROM table_name
    ORDER BY cost DESC
           , budget DESC
    

    So, you can well drop the rank_score alltogether, unless off course you plan to make experiments with various parameter values.

    As others have pointed, it’s not best practise to have a field that stores not data but a calculation. It’s de-normalization. Instaed, you keep the table normalized and let the database do the calculations every time you need it:

    SELECT id, budget, cost, 
           cost*1000 + budget*1 AS rank_score_calculated
    FROM table_name
    ORDER BY rank_score_calculated DESC
    

    rank_score_calculated is not stored in the above example. This way, you won’t have to update the calculated field every time a budget or a cost is changed or a new row is added in the table.

    There is only one drawback. If the table is really big and you need that query (and the calculation) done by lots of users and very often, and the table is updated quite often, then it may slow your database. In that case, one should start thinking about adding such a field.

    The other case is when one needs an absolute rank across all the table rows, like your need. Because MySQL has no “window” functions, it’s very hard to write such a query in pure SQL.)


    The rank can be calculated using MySQL variables

    SELECT *
         , @rownum:=@rownum+1 AS rank_calculated
    FROM table_name
       , (SELECT @rownum:=0) AS st
    ORDER BY rank_score DESC
    

    And if you want to put those values into rank, use:

    UPDATE table_name
             JOIN
             ( SELECT id
                    , @rownum:=@rownum+1 AS rank_calculated
               FROM table_name
                  , (SELECT @rownum:=0) AS st
               ORDER BY rank_score DESC
             ) AS r
             ON r.id = table_name.id
    SET table_name.rank = r.rank_calculated ;
    

    The above two queries are not pure SQL. You may examine the option to move to another daabase system that supports window functions, like Postgres, SQL-Server or Oracle.

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

Sidebar

Related Questions

Hello StackOverflow community, I have run into a problem that quite frankly is baffling
Hello stackoverflow community ! I am trying to figure out how to architect my
Hello StackOverflow community, Using Google App Engine, I wrote a keyToSha256() method within a
Hello StackOverflow'ers, I have a (flex) app that, on the click of a button,
Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
Hello StackOverflow community, The air.swf file referenced here: http://livedocs.adobe.com/flex/3/html/help.html?content=distributing_apps_3.html used to launch AIR applications
Hello StackOverflow community, Is there any way to open a folder in Windows Explorer
Hello StackOverflow community, Is there any way to package the installer for an Adobe
Hello StackOverflow Community, <plist> <dict> <key>Non Random Key</key> <dict> <key>Random Key</key> <dict> <key>Hello</key> <string>Hey</string>
Hello, stackoverflow community! I am working on a rather large database-driven web application. The

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.