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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…