将NULL值排序到表的末尾 [英] Sort NULL values to the end of a table
本文介绍了将NULL值排序到表的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
推荐答案
首先,空值 在默认情况下排在最后 ascending 命令。您无需执行任何其他操作。
First of all, NULL values are sorted last in default ascending order. You don't have to do anything extra.
此问题适用于 降序 顺序,这是完美的选择逆,因此首先对NULL值排序。 解决方案@Mosty指出与 PostgreSQL 8.3 :
ORDER BY somevalue DESC NULLS LAST
对于没有此标准SQL功能的 PostgreSQL 8.2 和更旧版本或其他RDBMS,您可以替代:
For PostgreSQL 8.2 and older or other RDBMS without this standard SQL feature you can substitute:
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.
稍后再介绍答案:
- PostgreSQL sort by datetime asc, null first?
这篇关于将NULL值排序到表的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文