views:

140

answers:

4

In Mysql, when you execute a select SQL statement, there is a default ordering if you don't include a sorting clause, how to reverse the default ordering? Just add DESC?

+2  A: 

There is no guaranteed order if you don't specify an ORDER BY clause, thus the 'reverse of the default order' is undefined.

Amber
+1  A: 

I think you would be better served by specifying the order you actually want. Tables, by their nature, have no order. It is probably just displayed in the order in which the rows were inserted - though there's no guarantee it will stay in that order.

Chances are, you probably just want to add this:

ORDER BY id DESC

...since most of the time, people use an auto-incrementing field called "id"

nickf
But what a pity, there is no "Id" field in the table.
Steven
But if there is no Id field, or equivalent, is the order meaningful?
pavium
I think the default order in which the rows were inserted, the default order is "First inserted, last out". I want to change it to "First inserted, first out".
Steven
If there is no logical timestamp nor some kind of increment field, then you have insufficient information available to determine the historic order of inserts.
micahwittman
+1  A: 

If you want the data to come out consistently ordered, you have to use ORDER BY followed by the column(s) you want to order the query by. ASC is the default, so you don't need to specify it. IE:

ORDER BY your_column

...is the equivalent to:

ORDER BY your_column ASC

ASC/DESC is on a per column basis. For example:

ORDER BY first_column, second_column DESC

...means that the query will sort the resultset as a combination using the first_column in ascending order, second_column in descending order.

OMG Ponies
The order I need is the reverse order in which the rows were inserted. "First inserted, first out".
Steven
@Steven: You'll have to provide the output of `DESCRIBE [your table name here]` from your database before I can suggest what to use. If you have an autonumber primary key column - order by it `ASC`. Next best thing would be a date_created column, using the datetime datatype. Again, `ASC`.
OMG Ponies
+1  A: 

Unless you can specify a column name in an ORDER BY clause, you can't use DESC, and you'll have to resort to tricks involving LIMIT to see the last few records.

This would be unsatisfactory, I think.

pavium