首页 > 解决方案 > 仅将非空值插入 SQL 表

问题描述

我尝试将值插入到我的表中,但我只想插入非空值,我需要忽略的空值不需要处理。我要插入的变量是 SQL 选择的结果,我试过了,但没有用:

insert into mytable(id)
values (case when (select id from tab2 where login=?) is not null then (select id from tab2 where login=?)) 

换句话说,我想插入与此条件相对应且不仅为空的 id。

标签: sqlpostgresqltalend

解决方案


这是你如何做到的:

insert into mytable(id)
select id from tab2 
where login=? and id is not null

推荐阅读