Reputation:
I need to display what the table contains from the freshest data to the oldest. Something like this doesn't work:
SELECT * FROM table ORDER BY DESC;
. I know its becouse after ORDER BY
should be name of the column. But I want to just reverse normal order (by normal I mean from the oldest to the freshest data). How to do this?
Upvotes: 0
Views: 4268
Reputation: 21905
The physical ordering of the records in a table are not guaranteed to match the sequence in which they were created. To do this, you will need to find or create a field you can sort on. A 'create date' field, or perhaps an id value which increases as new records are added (like an order id or something).
Upvotes: 0
Reputation: 40224
You could create a new field of type timestamp
and set the default value to CURRENT_TIMESTAMP
, then then ORDER BY
on that field.
Upvotes: 0
Reputation: 6636
You need to add a column for an insertion date or an incrementing key.
You can't rely on the physical storage pattern to give you a correct ordering. According to the MySQL documentation, there is no guarantee that rows returned will be in any particular order.
"...the result rows are displayed in no particular order. It is often easier to examine query output when the rows are sorted in some meaningful way. To sort a result, use an ORDER BY clause."
http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
Upvotes: 0
Reputation: 23373
In your query the DESC stands for descending, the reverse is ascending, or:
SELECT * FROM table ORDER BY column ASC;
btw, if you do not specify a column, what you call "normal order" really is random unless you specify an ordering.
Upvotes: 5
Reputation: 33511
The "normal order" is not always from oldest to freshest, since some records may be deleted and then these are replaced with the new ones. It means that the "natural order" may appear to be somewhat "random" with the freshest items being in the middle of the dataset.
Upvotes: 0