PostgreSQL空值null参与运算的处理方法
PostgreSQL空值null参与运算的处理方法
在数字加减乘除运算中遇到某个字段值为空值(null)的时候,输出的结果往往会让我们失望,得不到我们所期待的数值,可以通过 coalesce方式将凡是取值为null,即空值的字段转换成默认的值进行运算,从而改善运算效果。
coalesce函数用法: coalesce(字段名,默认值),很简单吧~
演示如下: [root@dbserver ~]# su - postgres -bash-3.2$ psql music psql (9.5beta2) 输入 "help" 来获取帮助信息. music=# create table test_null(id int,num1 int,num2 int); CREATE TABLE
插入数据,有的取值为正常数字,有的则为空值null: music=# insert into test_null values(1,100,100); INSERT 0 1 music=# insert into test_null values(2,200,200); INSERT 0 1 music=# insert into test_null values(3,null,100); INSERT 0 1 music=# insert into test_null values(4,300,null); INSERT 0 1 music=# insert into test_null values(5,null,500); INSERT 0 1
查看一下表中的数据,发现null值的字段全部为空值: music=# select * from test_null; id | num1 | num2 ----+------+------ 1 | 100 | 100 2 | 200 | 200 3 | | 100 4 | 300 | 5 | | 500 (5 行记录)
直接运算查看结果,会发现凡是遇到空值的运算结果均为空: music=# select num1+num2 from test_null; ?column? ---------- 200 400 (5 行记录)
使用 coalesce函数之后便可处理null的问题: music=# select coalesce(num1,0)+coalesce(num2,0) from test_null; ?column? ---------- 200 400 100 300 500 (5 行记录)
在这里,coalesce设置的默认值为0,同样也可以设置为其他的数值,比如人体体温可以设置默认值为36,冰点为0,沸点为100等。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of
