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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:25:55+00:00 2026-06-12T10:25:55+00:00

So I found this great use: SELECT (@row:=@row+1) AS ROW, ID FROM TableA ,(SELECT

  • 0

So I found this great use:

SELECT (@row:=@row+1) AS ROW, ID  
FROM TableA ,(SELECT @row := 0) r   
ORDER BY ID DESC

The @row:=@row+1 works great, but I get the row ordered by the ID.

My tables look more like this:

SELECT (@row:=@row+1) AS ROW, ID , ColA, ColB, ColC
FROM TableA 
JOIN TableB on TableB.ID = TableA.ID
JOIN TableC on TableC.ID = TableA.ID 
WHERE ID<500
,(SELECT @row := 0) r   
ORDER BY ID DESC

Note:
I noticed that if I remove the JOINs I DO get the requested result (In Which ROW is the sequential number of each row, no matter the ORDER BY of ID). The first example works great but for some reaosn, the JOINs mess it up somehow.

so I get this:

ROW  | ID  
3      15  
2      10  
1      2

What I am after is:

ROW  | ID  
1      15  
2      10  
3      2

Here’s the SqlFiddle

So it basically seems that the row number is evaluated before the ORDER BY takes place. I need the ORDER BY to take place after row was given.

How can I achieve that?

  • 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-12T10:25:56+00:00Added an answer on June 12, 2026 at 10:25 am

    Remove the ORDER BY:

    SELECT (@row:=@row+1) AS ROW, ID  
    FROM table1 ,(SELECT @row := 0) r   
    

    See SQL Fiddle with Demo

    Then if you want to use an ORDER BY wrap the query in another SELECT:

    select *
    from 
    (
      SELECT (@row:=@row+1) AS ROW, ID  
      FROM table1 ,(SELECT @row := 0) r
    ) x
    order by row
    

    Or if you leave the ORDER BY on the query, then you can see the way the row number is being applied by simply playing with either DESC or ASC order – See Demo

    If you use DESC order

    SELECT (@row:=@row+1) AS ROW, ID  
    FROM table1, (SELECT @row := 0) r
    order by id desc;
    

    the results are which appears to be the result you want:

    ROW | ID
    ----------
    1   | 15
    2   | 10
    3   | 2
    

    If you use ASC order:

    SELECT (@row:=@row+1) AS ROW, ID  
    FROM table1 ,(SELECT @row := 0) r   
    ORDER BY ID;
    

    the results are:

    ROW | ID
    ----------
    1   | 2
    2   | 10
    3   | 15
    

    Edit, based on your change, you should place the row number in a sub-query, then join the other tables:

    select *
    from
    (
      SELECT (@row:=@row+1) AS ROW, ID  
      FROM Table1,(SELECT @row := 0) r   
      order by ID desc
    ) x
    JOIN Table2 
      on x.ID = Table2.ID;
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I found this great class, all works well except... I spcify the output directory
I found this great code snippet that works wonderfully during circumstances that change a
I found this great Tablesorter plugin for jQuery but I can't make it work
I have found many great answers to this from a year ago (when it
Say I have this great query in my stored procedure. Select * from Temp
I recently answered this question how-to-call-user-defined-function-in-order-to-use-with-select-group-by-order-by My answer was to use an inline view
I found this great gem 'method finder' that I'm trying to use to help
I found this great example of horizontal parallax http://www.perandersen.no/sandbox/parallax/ The only modification I would
I have just found this great tutorial as it is something that I need.
I found this question which is a great starting point towards creating embedded widgets

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.