4

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?

Ted
  • 3,805
  • 14
  • 56
  • 98

2 Answers2

10

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

Taryn
  • 242,637
  • 56
  • 362
  • 405
  • Sorry, I had to unmark your answer because admins did not allow me to ask in another question. The thing is that when JOINs are involved, it somehow messes the result. so that ID=2 always gets the ROW=1 and ID=15 get ROW=3. explained in the :edit: – Ted Oct 03 '12 at 15:53
  • @ted well that is a separate question from the original one you asked. please create a sql fiddle with some sample data – Taryn Oct 03 '12 at 15:55
  • I am sorry about that bluefeet, it was moderators decision – Ted Oct 03 '12 at 16:02
  • @Ted see my edit, you will want to use a subquery to assign the row number and then join on it. – Taryn Oct 03 '12 at 16:05
  • @Ted can you explain "moderators decision"? Are you getting the message "we are no longer accepting questions from this account"? If so, [please read this in full](http://meta.stackexchange.com/questions/86997/what-can-i-do-when-getting-sorry-we-are-no-longer-accepting-questions-answers). – Aaron Bertrand Oct 03 '12 at 16:09
  • I decided to mark as answered and asked a new question with the modifications. It was closed as a duplicate of this thread. – Ted Oct 04 '12 at 12:45
4

I dont find any problem with your query

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


SQL Fiddle demo

Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58