How to reverse the default ordering in Mysql?

前端 未结 5 1448
名媛妹妹
名媛妹妹 2020-12-20 18:29

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 DE

相关标签:
5条回答
  • 2020-12-20 19:04

    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.

    0 讨论(0)
  • 2020-12-20 19:06

    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"

    0 讨论(0)
  • 2020-12-20 19:14

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

    0 讨论(0)
  • 2020-12-20 19:22

    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.

    0 讨论(0)
  • 2020-12-20 19:26

    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.

    0 讨论(0)
提交回复
热议问题