为什么在PostgreSQL查询中排序DESC时NULL值最先出现? [英] Why do NULL values come first when ordering DESC in a PostgreSQL query?

查看:1563
本文介绍了为什么在PostgreSQL查询中排序DESC时NULL值最先出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在排序查询降序或升序时,您最好先选择NULLS?



在我看来,绝大多数不管排序升序还是降序的期望行为将是最后一行。

解决方案

实际上,使用默认排序顺序( ASCENDING )NULL值来自最后



逻辑指示排序顺序与 DESCENDING 关键字,因此在这种情况下,首先会出现NULL



但最好的部分是最后一个:你想要的方式:





引用 当前手动,版本9.3写作:


$如果指定 NULLS LAST ,则null值将在所有非空的
值后排序; b $ b <如果指定 NULLS FIRST ,空值将在所有
非空值之前排序。如果没有指定,当指定或暗示 ASC 时,默认行为是
NULLS LAST 当指定 DESC
时, NULLS FIRST 好像null比$ null更大
)。当指定 USING 时,排序
的默认空值取决于运算符是小于还是大于运算符。


大胆强调我。


When would you ever want NULLS first when ordering a query descending or ascending?

In my opinion, the vast majority of the time the desired behavior whether sorting ascending or descending would be NULLS LAST. Instead, we should have to specify NULLS FIRST.

解决方案

Actually, with default sort order (ASCENDING) NULL values come last.

Logic dictates that the sort order be reversed with the DESCENDING keyword, so NULLs come first in this case.

But the best part comes last: you can choose which way you want it:

Quoting the current manual, version 9.3 as of writing:

If NULLS LAST is specified, null values sort after all non-null values; if NULLS FIRST is specified, null values sort before all non-null values. If neither is specified, the default behavior is NULLS LAST when ASC is specified or implied, and NULLS FIRST when DESC is specified (thus, the default is to act as though nulls are larger than non-nulls). When USING is specified, the default nulls ordering depends on whether the operator is a less-than or greater-than operator.

Bold emphasis mine.

这篇关于为什么在PostgreSQL查询中排序DESC时NULL值最先出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆