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

The Archive Base Latest Questions

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

suppose I have SELECT * FROM t ORDER BY j is there a way

  • 0

suppose I have

SELECT * FROM t ORDER BY j

is there a way to specify the query to also return an autoincremented column that go along with the results that specifies the rank of that row in terms of the ordering?

also this column should also work when using ranged LIMITs, eg

SELECT * FROM t ORDER BY j LIMIT 10,20

should have the autoincremented column return 11,12,13,14 etc….

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

    Oracle, MSSQL etc support ranking functions that do exactly what you want, unfortunately, MySQL has some catching up to do in this regard.

    The closest I’ve ever been able to get to approximating ROW_NUMBER() OVER() in MySQL is like this:

    SELECT t.*, 
           @rank = @rank + 1 AS rank
    FROM t, (SELECT @rank := 0) r
    ORDER BY j
    

    I don’t know how that would rank using ranged LIMIT unless you used that in a subquery perhaps (although performance may suffer with large datasets)

    SELECT T2.*, rank
    FROM (
        SELECT t.*, 
               @rank = @rank + 1 AS rank
        FROM t, (SELECT @rank := 0) r
        ORDER BY j
        ) t2
    LIMIT 10,20
    

    The other option would be to create a temporary table,

    CREATE TEMPORARY TABLE myRank
    (
         `rank` INT(11) NOT NULL AUTO_INCREMENT,
         `id` INT(11) NOT NULL,
         PRIMARY KEY(id, rank)
    )
    
    INSERT INTO myRank (id)
    SELECT T.id
    FROM T 
    ORDER BY j
    
    SELECT T.*, R.rank
    FROM T
        INNER JOIN myRank R 
            ON T.id = R.id 
    LIMIT 10,20
    

    Of course, the temporary table would need to be persisted between calls.

    I wish there was a better way, but without ROW_NUMBER() you must resort to some hackery to get the behavior you want.

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

Sidebar

Related Questions

There is a mysql query. suppose : select primary_field, field2 from mytable order by
Suppose I have the following query: select * from A, B, C, D where
Suppose I have a datetime column. SELECT * FROM mytable WHERE thetime < INTERVAL
Suppose I have a SELECT statement that returns some set of results. Is there
Suppose I have a datetime column in MySQL. How do I select all that
Suppose I have a MySQL query with two conditions: SELECT * FROM `table` WHERE
Suppose I have this query SELECT * FROM ( SELECT * FROM table_a WHERE
So i have a query that is suppose to get me data from two
Suppose I have a column called fruits I want to select all of the
Here is the code that I currently have: SELECT `A`.* FROM `A` LEFT JOIN

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.