I have an sql statement as below attempting to retrieve the most recent entries in a table. I have two questions:
-
Is it better to order by id or by date?
-
How do I rewrite this sql statement to re-order by date?
SELECT id, comment, DATE_FORMAT(entry_date, '%W %H:%i') FROM comments ORDER BY id DESC LIMIT 10
It depends on what you mean by most recent:
If you mean the most recently created record, then (in most cases) by primary key id will work.
If you mean the most recently updated record, then definitely by date.
To sort by date just change the field name:
ORDER BY entry_date DESC