如何反转Mysql中的默认排序?

时间:2022-01-11 22:48:26

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?

在Mysql中,当你执行一个select SQL语句时,如果你不包含一个排序子句,有一个默认排序,如何反转默认排序?只需添加DESC?

5 个解决方案

#1


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,然后使用要对其进行排序的列。 ASC是默认值,因此您无需指定它。 IE:

ORDER BY your_column

...is the equivalent to:

......相当于:

ORDER BY your_column ASC

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

ASC / DESC基于每列。例如:

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.

...表示查询将使用first_column按升序排序结果集,second_column按降序排序。

#2


3  

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

如果未指定ORDER BY子句,则没有保证顺序,因此未定义“默认顺序的反向”。

#3


3  

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.

我认为它会按你的意愿工作。

#4


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"

...从大多数时候开始,人们使用一个名为“id”的自动递增字段

#5


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.

除非您可以在ORDER BY子句中指定列名,否则不能使用DESC,并且您必须使用涉及LIMIT的技巧来查看最后几条记录。

This would be unsatisfactory, I think.

我认为这不能令人满意。

#1


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,然后使用要对其进行排序的列。 ASC是默认值,因此您无需指定它。 IE:

ORDER BY your_column

...is the equivalent to:

......相当于:

ORDER BY your_column ASC

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

ASC / DESC基于每列。例如:

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.

...表示查询将使用first_column按升序排序结果集,second_column按降序排序。

#2


3  

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

如果未指定ORDER BY子句,则没有保证顺序,因此未定义“默认顺序的反向”。

#3


3  

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.

我认为它会按你的意愿工作。

#4


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"

...从大多数时候开始,人们使用一个名为“id”的自动递增字段

#5


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.

除非您可以在ORDER BY子句中指定列名,否则不能使用DESC,并且您必须使用涉及LIMIT的技巧来查看最后几条记录。

This would be unsatisfactory, I think.

我认为这不能令人满意。