今天碰到一个问题,运维怕数据库已经有了这条数据,怕重复,让我insert加个条件判断一下,我查了才发现,竟然没有where,我找到的方法如下
INSERT INTO test(id, name, note) select 123, '测试', '备注' from dual
where (select count(id) from test where id = '123' and name= '测试') = 0;
本文介绍了一种使用SQL语句在插入数据前进行条件判断的方法,以避免数据重复。通过一个具体的例子展示了如何利用子查询来检查目标记录是否已存在于目标表中。
今天碰到一个问题,运维怕数据库已经有了这条数据,怕重复,让我insert加个条件判断一下,我查了才发现,竟然没有where,我找到的方法如下
INSERT INTO test(id, name, note) select 123, '测试', '备注' from dual
where (select count(id) from test where id = '123' and name= '测试') = 0;