5.postgresql--COALESCE

在 PostgreSQL 中, COALESCE函数返回第一个非空参数。它通常与 SELECT 语句一起使用以有效处理空值。

COALESCE函数接受无限数量的参数。它返回第一个不为空的参数。如果所有参数都为 null,则 COALESCE函数将返回 null。 COALESCE函数从左到右计算参数,直到找到第一个非空参数。不评估第一个非空参数中的所有剩余参数。

sql 复制代码
CREATE TABLE items (
    ID serial PRIMARY KEY,
    product VARCHAR (100) NOT NULL,
    price NUMERIC NOT NULL,
    discount NUMERIC
);

INSERT INTO items (product, price, discount)
VALUES
    ('A', 1000, 10),
    ('B', 1500, 20),
    ('C', 800, 5),
    ('D', 500, NULL);
复制代码
   select product,(price-discount) as net_price from items


select product,(price-coalesce(discount,0)) as net_price from items

相关推荐
春生野草1 天前
SpringBoot配置文件
java·数据库·spring boot
奇点 ♡1 天前
MySQL基础题
数据库·sql·mysql
唐古乌梁海1 天前
【mysql】MySQL 数据库迁移
数据库·mysql·adb
啊吧怪不啊吧1 天前
SQL之表的时间类内置函数详解
大数据·服务器·数据库·sql
2503_928411561 天前
11.5 包和包管理器
数据库·arcgis·node.js·编辑器
JanelSirry1 天前
真实场景:防止缓存穿透 —— 使用 Redisson 布隆过滤器
数据库·mysql·缓存·redisson·布隆过滤器
mmm.c1 天前
mysql启动提示1067:进程意外终止
数据库·mysql
埃泽漫笔1 天前
Redis单线程还是多线程?
数据库·redis·缓存
TDengine (老段)1 天前
TDengine 产品组件 taosX
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
沐伊~1 天前
mysql 安装
数据库·mysql