将 NULL 值排序到表的末尾 [英] Sort NULL values to the end of a table
问题描述
PostgreSQL 有没有办法将字段中具有 NULL
值的行排序到所选表的末尾?
Is there a way with PostgreSQL to sort rows with NULL
values in fields to the end of the selected table?
喜欢:
SELECT * FROM table ORDER BY somevalue, PUT_NULL_TO_END
推荐答案
NULL
值 以默认升序顺序排在最后.您无需做任何额外的事情.
NULL
values are sorted last in default ascending order. You don't have to do anything extra.
该问题适用于 降序 顺序,这是完美的逆序,因此将 NULL
值排在最前面.
PostgreSQL 8.3 引入了NULLS LAST
:
The issue applies to descending order, which is the perfect inverse and thus sorts NULL
values on top.
PostgreSQL 8.3 introduced NULLS LAST
:
ORDER BY somevalue DESC NULLS LAST
对于PostgreSQL 8.2 及更早版本或其他RDBMS 没有此标准 SQL 功能:
For PostgreSQL 8.2 and older or other RDBMS without this standard SQL feature:
ORDER BY (somevalue IS NULL), somevalue DESC
FALSE
在 TRUE
之前排序,所以 NULL
值排在最后,就像上面的例子一样.
FALSE
sorts before TRUE
, so NULL
values come last, just like in the example above.
见:
这篇关于将 NULL 值排序到表的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!