10

In MySQL, when you execute a select SQL statement, there is a default ordering if you don't include a sorting clause. How can I reverse the default ordering? Just add DESC?

3
  • 2
    Duplicate: stackoverflow.com/questions/1793147/… Commented Nov 27, 2009 at 6:34
  • 1
    There is no such thing as a "default ordering" so you cannot "reverse" it. Commented Mar 17, 2014 at 17:32
  • This question is similar to: SQL best practice to deal with default sort order. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 26, 2024 at 4:26

5 Answers 5

7

You can set a counter in your result fields and sort using it:

SELECT *, @counter := @counter + 1 AS 'counter' FROM tableName, (SELECT @counter := 0) r ORDER BY counter DESC

I think it will work as you want.

Sign up to request clarification or add additional context in comments.

1 Comment

@Jerry But this is just the reverse of whatever order the rows were retrieved in, which doesn't have any guaranteed connection to the implementation order, which in turn has no guarantee to be anything in particular. If you want an order guaranteed, it has to be in the data. Also there are no guarantees for queries that both set & read a variable, so this query has undefined behaviour. For a certain recent build of 5.7 people at Percona who looked at the implementation saw that certain use of variables in case expressions (not like this) gives certain predictable behaviour. Until.
5

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.

2 Comments

The order I need is the reverse order in which the rows were inserted. "First inserted, first out".
@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.
3

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

1 Comment

For those who prefer answers with evidence to back up the claims made: stackoverflow.com/questions/8746519/…
1

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"

4 Comments

But what a pity, there is no "Id" field in the table.
But if there is no Id field, or equivalent, is the order meaningful?
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".
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.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.