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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:42:28+00:00 2026-05-30T21:42:28+00:00

My previous search results (containing MySQL – row number in recordset? that really helped

  • 0

My previous search results (containing MySQL – row number in recordset? that really helped me) was not satisfying because sorting by other columns overrides the native sort of @RowNumber.

Consider my simplified member table below:

row_number | id | firstname | lastname
--------------------------------------
1          | 1  | Steve     | Jobs
2          | 2  | Bill      | Gates
3          | 3  | Rasmus    | Lerdorf
4          | 4  | Linus     | Torvalds

resulted by the following query:

SELECT id, firstname, lastname, @RowNumber = @RowNumber + 1 AS row_number
FROM member, (SELECT @RowNumber := 0) AS counter_table

But when I’m going to sort based on some columns other than row_number or id, the row_number‘s sort will corrupt:

SELECT id, firstname, lastname, @RowNumber = @RowNumber + 1 AS row_number
FROM member, (SELECT @RowNumber := 0) AS counter_table
ORDER BY firstname DESC

Result:

row_number | id | firstname | lastname
--------------------------------------
2          | 2  | Bill      | Gates
4          | 4  | Linus     | Torvalds
3          | 3  | Rasmus    | Lerdorf
1          | 1  | Steve     | Jobs

Note: None of the table, queries and any other thing are tested examples. Only the problem itself is in the real world!

I supposed those simple situations to be easy to understand and resolve. (If they were any problem, please forget about those little mistakes and consider the important problem itself)

  • 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-30T21:42:30+00:00Added an answer on May 30, 2026 at 9:42 pm

    Your implementation on row_number is dependent of the results being returned from the database already in the order in which you are displaying them.

    The reason that it fails when you order by firstname is because the results are being returned from the database in order of the primary key, so id 1 get row_number 1, then they’re ordered via firstname later (via filesort).

    If you add an index on the firstname column, then the results will be fetched from the database ordered by firstname instead of the primary key, so the row numbers will be fine.

    If you do an EXPLAIN on your query you will notice that it’s probably using the primary key as the index, and using filesort to order the results because it doesn’t have an index on firstname. The order occurs after the results are fetched from the table.

    Adding an index on firstname, you’ll see that it will use the index on firstname to fetch the results from the database already ordered by firstname, so the first name will have row_number 1.

    To play it safe, you’ll probably want to force the index.

    However, this is strictly academic to explain what’s occurring and how you might work around the issue.

    The most likely solution would be to perform your first query as a subquery, and add the row number in an outer query, after the results have already been ordered.
    Some applications will reissue a query any time the user wants to change the sort order, however, some applications will cache the results, order them in the application, and generate the row numbers in the application.

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

Sidebar

Related Questions

I'm programming a site in PHP/MySQL that gets search results for products via API
I want to implement a search that will rank results higher, if previous, similar
I have a website that returns search results from Twitter, written in C# ASP.NET.
I have a table that returns results from a search. When the user search
Does it mean search the previous folder for somefile.h or the project folder for
In previous versions of Excel there was a registry entry that you could create
From previous post, I learnt that for there are two ways, at least, to
From previous experience I had been under the impression that it's perfectly legal (though
In previous question of mine, someone had meantioned that using Semaphores were expensive in
In previous versions of Visual Studio I remember I saw the error code/number in

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.