插入时为了方便,更是为了安全(避免查询==0? insert 这种方式),我们使用where not exists
下面代码:
-- 创建测试表
create table testage(
id int not null,
age int );
-- 插入三条数据
insert into testage(id,age) values(1,22);
insert into testage(id,age) values(2,23);
insert into testage(id,age) values(3,24);
-- 这里插入一条
insert into testage(id,age)
select 4,25 from dual
where not exists(select count(1) from testage where age=25);
猜一下执行结果?

是的,显示执行成功0,为什么?
我们单独执行这条sql,结果如图:

0存在吗?是存在的。需要更换为此种写法:

因为在插入之前,age=25是不存在的,查询是null
4997

被折叠的 条评论
为什么被折叠?



