为什么在PostgreSQL查询中排序DESC时NULL值最先出现? [英] Why do NULL values come first when ordering DESC in a PostgreSQL query?
问题描述
在排序查询降序或升序时,您最好先选择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; ifNULLS FIRST
is specified, null values sort before all non-null values. If neither is specified, the default behavior isNULLS LAST
whenASC
is specified or implied, andNULLS FIRST
whenDESC
is specified (thus, the default is to act as though nulls are larger than non-nulls). WhenUSING
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屋!