postgres 判断null_postgresql基础:null的那些坑

本文通过一个线上bug实例,探讨了在PostgreSQL中null比较的特殊性,以及如何处理null值的问题。当使用ORM生成SQL如`NOT IN (NULL)`时,查询结果为空,原因是null无法与其他任何值进行比较。解决方案是在生成查询条件时,考虑数组是否为空或包含null。同时,文章提醒在进行null判断时,应使用`is null`和`is not null`,并指出`select count(type)`不包含null值,以及`type != 1`不包括null的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这篇写的是postgres,但是实际上我简单试了对于mysql也是适用的.

先简单介绍下背景,这是前几天线上的一个真实bug. 很久之前产品经理要求某个列表页排除某些类型,开发一气呵成写完上线,非常完美,突然前几天产品说现在不需要排除这几个类别了,就把要排除的配置项给清空了,结果发现列表页也空了,造成大事故啊。

图文无关

我检查发现问题代码大致如下const products = models.Product.findAll({

productType: { $notIn: excludedTypeIds },

})

看着毫无bug是不是?ORM最终出来的的sql大致是select * from product where product_type NOT IN (1,2,3,4)

但是考虑下excludedTypeIds是空数组的情况,SQL会变成select * from product where product_type NOT IN (NULL)

看着似乎也没问题,这个sql的结果应该是所有product的列表吧?

实际上不是的,这个查询的结果为空。问题就出在NULL上.

在postgresql里,null是不可比较的,不管什么跟NULL比较的结果还是NULL,如下select 1 !=null, 1 = null, null = null, null != null, 1 in (null), 1 not in (null)

sql null

这个sql我在mysql里也试了下,结果是一样的.

到这里就很好理解之前的那个 NOT IN (NULL)了,因为这个表达式结果为NULL,那么自然就一行结果都没有了.

最终这个程序要修复就很简单了,如果是空数组就不要添加这个where条件,大致如下const where = {}

if (excludedTypeIds.length) {

where.productType = { $notIn: excludedTypeIds }

}

const products = models.Product.findAll({

where,

})

这个处理其实还不是很严格,实际上要考虑数组里可能有null的情况.

最后总结下关于null的其他需要注意的地方

基本常识: null判断用is null is not null而不是 id = null, id != null

select count(type) from product这里是没有计算type为null的行的

select * from product where type != 1并不包含type为null的,这个从上面关于null判断里就可以知道了

WARN[0000] /paperless-ngx/docker-compose.yml: `version` is obsolete name: paperless-ngx services: broker: image: redis:7 networks: default: null restart: unless-stopped volumes: - type: volume source: redisdata target: /data volume: {} db: environment: POSTGRES_DB: paperless POSTGRES_PASSWORD: paperless POSTGRES_USER: paperless image: postgres:15 networks: default: null restart: unless-stopped volumes: - type: volume source: pgdata target: /var/lib/postgresql/data volume: {} webserver: depends_on: broker: condition: service_started required: true db: condition: service_started required: true environment: PAPERLESS_DBHOST: db PAPERLESS_DBNAME: paperless PAPERLESS_DBPASS: paperless PAPERLESS_DBUSER: paperless PAPERLESS_REDIS: redis://broker:6379 env_file: .env healthcheck: test: - CMD - curl - -f - http://localhost:8000 timeout: 10s interval: 30s retries: 5 image: ghcr.io/paperless-ngx/paperless-ngx:latest networks: default: null ports: - mode: ingress target: 8000 published: "8000" protocol: tcp restart: unless-stopped volumes: - type: volume source: data target: /usr/src/paperless/data volume: {} - type: volume source: media target: /usr/src/paperless/media volume: {} - type: bind source: /paperless-ngx/data/export target: /usr/src/paperless/export bind: create_host_path: true - type: bind source: /paperless-ngx/data/consume target: /usr/src/paperless/consume bind: create_host_path: true networks: default: name: paperless-ngx_default volumes: data: name: paperless-ngx_data media: name: paperless-ngx_media pgdata: name: paperless-ngx_pgdata redisdata: name: paperless-ngx_redisdata
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值