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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:07:33+00:00 2026-06-17T07:07:33+00:00

Let’s say I have a table named members with this structure: member_id, col1, col2,

  • 0

Let’s say I have a table named members with this structure:

member_id, col1, col2, number

Now, if I run this query:

SELECT @rownum:=@rownum+1 number, member_id 
FROM members, (SELECT @rownum:=0) r ORDER BY col1 DESC, col2 DESC

I get a number for each row. Is it possible to assign this number to column number for each row?

For example, if the table contained this data:

1 1000 1.2 0
2 8700 1.1 0
3 1000 1.1 0

First row number column should be set to 2, second row to 1 and third row to 3.

  • 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-17T07:07:35+00:00Added an answer on June 17, 2026 at 7:07 am

    You may use a temporary table to store the values and UPDATE from it with a join.

    /* Store query result in a temporary table */
    CREATE TEMPORARY TABLE ranks AS
      SELECT @rownum:=@rownum+1 number, member_id FROM members, (SELECT @rownum:=0) r ORDER BY col1 DESC, col2 DESC;
    
    /* And join against it to update your members.number column */
    UPDATE 
      members
      JOIN ranks ON members.member_id = ranks.member_id
    SET members.number = ranks.number;
    
    DROP TABLE ranks;
    

    Actually by my test just now I was able to do this without the temporary table. I didn’t think MySQL would permit updating the same table from a subquery but apparently it is permitted with the correlated subquery.

    UPDATE 
      members 
      /* JOIN against a subquery which returns the rank values */
      JOIN ( 
       SELECT @rownum:=@rownum+1 number, member_id 
       FROM members, (SELECT @rownum:=0) r
       ORDER BY col1 DESC, col2 DESC
      ) ranks ON members.member_id = ranks.member_id 
    /* And set the number column to the rank */
    SET members.number = ranks.number;
    

    SQLfiddle.com demo

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

Sidebar

Related Questions

Let's say I have a table with a Color column. Color can have various
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have pseudocode like this: main() { BOOL b = get_bool_from_environment(); //get
Let's say I have an actor like this (Using Akka 2.1 with Scala 2.10):
Let's say I have some text as follows: do this, do that, then this,
Let's say I have a main folder in my website named test which contains
Let's say you have a class library project that has any number of supplemental
Let me frame it this way.. Say I have an application server running on
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012

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.