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

The Archive Base Latest Questions

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

Possible Duplicate: Need a sequence number for every row in MySQL query So I

  • 0

Possible Duplicate:
Need a sequence number for every row in MySQL query

So I found this great use:

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

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 
,(SELECT @row := 0) r   
ORDER BY ID DESC

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

so I get something more like this:

ROW  | ID  
3      15  
2      10  
1      2

What I am after is:

ROW  | ID  
1      15  
2      10  
3      2

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)

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.

Any idea regarding how I can achieve that, and what do the JOINS do that messes it up?

Thx!

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

    I need the ORDER BY to take place after row was given.

    Then try this:

    SELECT *
    FROM
    (
       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 
        ,(SELECT @row := 0) r 
    ) t
    ORDER BY ROW ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: MySQL date format i have this date which i need to make
Possible Duplicate: Sending HTML email from PHP I need to send an html mail
Possible Duplicate: Why we need Thread.MemoryBarrier()? From O'Reilly's C# in a Nutshell: class Foo
Possible Duplicate: behaviour of const_cast I need to understand what this line means char
Possible Duplicate: Get column index from label in a data frame I need to
Possible Duplicate: Populate unique values in to VBA array from excel I need to
Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
Possible Duplicate: I need a good way to get data from a thread to
Possible Duplicate: Do I need to use mysql_close(connection)? Is it best to close a
Possible Duplicate: ZipCode from location I need to find zip code for current location.

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.