小编Aus*_*han的帖子

PostgreSQL 中的条件更新

我正在测试一些条件渲染,看看它在小规模上的表现是否符合预期,然后再在更大的表上实现。

目标是创建一个更新查询,该查询仅更新用户输入新数据的列,如果没有数据,则不对这些列执行任何操作

这是我测试时的情况。具有空值的行未更新newValue:

drop table if exists tester

create table tester (
id serial primary key
val TEXT,
)

insert into tester (val) values (null)

select * from tester

update tester 
set val = case 
when val = null 
then 'newValue' 
else val 
end;
Run Code Online (Sandbox Code Playgroud)

这是我想要生成的一个简单示例($1是用户输入):

drop table if exists tester

create table tester (
id serial primary key
val TEXT,
)

insert into tester (val) values (null)

select * from tester

update tester 
set val = …
Run Code Online (Sandbox Code Playgroud)

postgresql

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

postgresql ×1