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

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

问题描述

在对查询进行降序或升序排序时,您何时会首先需要 NULLS?

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

在我看来,无论是升序还是降序,绝大多数时间所需的行为都是 NULLS LAST.相反,我们应该首先指定 NULLS.

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.

推荐答案

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

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

逻辑要求使用 DESCENDING 关键字反转排序顺序,因此在这种情况下,NULL 首先.

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:

引用 current 手册,撰写本文时为 9.3 版:

Quoting the current manual, version 9.3 as of writing:

如果指定了NULLS LAST,则空值排在所有非空之后价值观;如果指定了 NULLS FIRST,则空值排在所有之前非空值.如果两者都没有指定,默认行为是NULLS LASTASC 被指定或暗示NULLS FIRSTDESC被指定(因此,默认值是充当空值更大的比非空值).当指定 USING 时,默认空值排序取决于运算符是小于还是大于运算符.

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.

粗体强调我的.

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

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