"Neeper" <ne****@hotmail.com> wrote in message
news:t6********************************@4ax.com...
I'm trying to pull the last 10 records from a transactions from a
table using this query:
SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10
But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;
You could do this with a subquery *if available* in your Version of MySQL.
SELECT *
FROM (SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10) As X
ORDER BY timestamp ASC;
But the use of temporary tables is equivalent and available further back in
MySQL revision history.
CREATE TEMPORARY TABLE X
SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10;
SELECT * FROM X ORDER BY timestamp ASC;
DROP TABLE X;
These (3) queries produce exactly the same result. I suspect that
subqueries simply use temporary tables behind the scenes anyway. You can
always do it yourself up front as shown.
Thomas Bartkus