在 PostgreSQL 中, COALESCE函数返回第一个非空参数。它通常与 SELECT 语句一起使用以有效处理空值。
COALESCE函数接受无限数量的参数。它返回第一个不为空的参数。如果所有参数都为 null,则 COALESCE函数将返回 null。 COALESCE函数从左到右计算参数,直到找到第一个非空参数。不评估第一个非空参数中的所有剩余参数。
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






![勒索病毒.[tsai.shen@mailfence.com].faust、.[support2022@cock.li].faust引起的数据被加密恢复](https://img-blog.csdnimg.cn/1cb61e7299d241738b9e270cc89a1e00.png)













