Nulls first/last功能简介
Nulls first/last功能主要用于order by排序子句中,影响空值Null在排序结果中的位置。简单来说,Nulls first表示Null值在排序时一直排在所有值的前面,也就是处理order by a desc时PostgresQL执行器认为Null值大于所有值,而order by a或order by a asc时执行器认为Null值小于所有值,将Null值排在前面。Nulls last表示Null值在排序时一直排在所有值的后面,也就是处理order by a desc时PostgresQL执行器认为Null值小于所有值,而order by a或order by a asc时执行器认为Null值大于所有值,将Null值排在前面。当不指定Nulls first/last功能时,执行器默认认为Null值要大于所有值,以此为依据处理order by子句的排序结果。

Nulls first/last功能简单展示
以下测试均为Postgres数据库下测试,数据库版本为9.2.2,测试系统为Linux。

普通表简单功能展示:

Create    table  test1(a int, b int);

Insertinto test1 values(1,2);

Insertinto test1 values(3,4);

Insertinto test1 values(5);

Select * from test1 order by b desc nulls first;

a    b

5

3    4

1    2

Select * from test1 order by b desc nulls last;

a    b

3    4

1    2

5

Select *from test1 order by b desc nulls;  报错

分区表简单功能展示,注意PostgresQL数据库建分区表的方式:

createtable test_hash(a int, b int);

createtable test_hash1(check(a>=0 and a<5)) inherits(test_hash);

createtable test_hash2(check(a>=5)) inherits(test_hash);

createrule test_hash_1 as on insert to test_hash where(a>=0 and a<5) do insteadinsert into test_hash1 values(NEW.a,NEW.b);

createrule test_hash_2 as on insert to test_hash where(a>=5) do instead insertinto test_hash2 values(NEW.a,NEW.b);

Insertinto test_hash values(1,2);

Insertinto test_hash values(3,4);

Insertinto test_hash values(5);

Select *from test_hash order by b desc nulls first;

a      b

5

3      4

2      2

Select *from test_hash order by b desc nulls last;

a      b

3      4

1      2

5

以上均是select语句中的order by子句进行的Nulls first/last功能展示,创建索引等其他需要order by子句的地方同理。这里只是简单的展示了一下功能,有时间会写一些剖析PostgresQL数据库的文章出来

PostgresQL中的NUlls first/last功能的更多相关文章

  1. PostgreSQL中的索引(一)

    引言 这一系列文章主要关注PostgreSQL中的索引. 可以从不同的角度考虑任何主题.我们将讨论那些使用DMBS的应用开发人员感兴趣的事项:有哪些可用的索引:为什么会有这么多不同的索引:以及如何使用 ...

  2. Postgresql中临时表(temporary table)的特性和用法

    熟悉Oracle的人,相比对临时表(temporary table)并不陌生,很多场景对解决问题起到不错的作用,开源库Postgresql中,也有临时表的概念,虽然和Oracle中临时表名字相同,使用 ...

  3. Postgresql中的数据类型大全

    一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...

  4. 用ArcMap在PostgreSQL中创建要素类需要执行”create enterprise geodatabase”吗

    问:用Acmap在PostgreSQL中创建要素类需要执行"create enterprise geodatabase"吗? 关于这个问题,是在为新员工做postgresql培训后 ...

  5. PostgreSQL中定时job执行(pgAgent)

    PostgreSQL中定时job执行 业务分析 近期项目需要定期清理数据库中的多余数据,即每月1号删除指定表中一年以上的数据. 初步分析这种定时job可以使用一下两种技术实现: Linux的cront ...

  6. 在PostgreSQL中 pg_start_backup 做了什么?

    # 在PostgreSQL中 pg_start_backup 做了什么?HM 2019-07-30 ## pg_start_backup 做一个备份开始标记,还做了一些其他的操作,下面进行探寻. * ...

  7. 在PostgreSQL中CREATE STATISTICS

    如果你用Postgres做了一些性能调优,你可能用过EXPLAIN.EXPLAIN向你展示了PostgreSQL计划器为所提供的语句生成的执行计划,它显示了语句所引用的表如何被扫描(使用顺序扫描.索引 ...

  8. 实现ABP中Person类的权限功能

    菜单项的显示功能已经完全OK了.那么我们就开始制作视图功能吧. 首先测试接口是否正常 我们通过代码生成器将权限和application中大部分功能已经实现了.那么我们来测试下这些接口ok不. 浏览/a ...

  9. 通过arcgis在PostgreSQL中创建企业级地理数据库

    部署环境: Win7 64位旗舰版 软件版本: PostgreSQL-9.1.3-2-windows-x64 Postgis-pg91x64-setup-2.0.6-1 Arcgis 10.1 SP1 ...

随机推荐

  1. django导入 views.py

    网上找了各种教程..均无解....自己摸索出来了..分享 给大家... 首先INSTALLED_APP 加入 自己的 app pili,然后如下图 from App名称 import views 就 ...

  2. 奇妙的 CSS几何图形

    三角形:通常会使用透明的border模拟出一个三角形:▲ .traingle { width:; height:; border-left: 50px solid transparent; borde ...

  3. Ubuntu server 修改系统时区

    执行命令: sudo dpkg-reconfigure tzdata

  4. day_06 再谈编码

    1. 小数据池(常量池) 1.id() 通过id()查询一个变量在内存中的地址 2.is和== 1.is 判断左右两端内存地址是否一致,如果返回值是TRUE,则可以判断这两个变量值是同一对象 2.== ...

  5. Go语言基础练习题系列1

    1.练习1 题目:使用fmt分别打印字符串.二进制.十进制.十六进制.浮点数. package main import ( "fmt" ) func main() { var a ...

  6. java拦截器的使用

    转载: https://www.cnblogs.com/liangblog/p/7234757.html https://blog.csdn.net/reggergdsg/article/detail ...

  7. windows下安装TensorFlow(CPU版)

    建议先到anaconda官网下载最新windows版的anaconda3.6,然后按步骤进行安装.(这里我就不贴图了,自己下吧) 1.准备安装包 http://www.lfd.uci.edu/~goh ...

  8. Connection closing...Socket close. Connection closed by foreign host. Disconnected from remote host(centos6.9) at 14:59:05.

    查找网上资料解决方法如下 [root@localhost ssh]# service sshd start Starting sshd: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  9. Dev Express Report 学习总结(七)Dev Express Reports 常见知识点总结

    1. CalculatedField中常见表达式: A. For percent: [ValA]* 100 / ([ValA]+[ ValB]);           PS. Please remre ...

  10. Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) F. Substrings in a String

    http://codeforces.com/contest/914/problem/F 以前做过一个类似的,但是查询的子串长度最多是10,这个时候就是用bit + hash做了.这是因为改变一个字符, ...