在插入数据库前进行判断再决定是否插入数据

--SQL_Server 适用方法
if not exists (select * from table1 where mid='aa') 
INSERT INTO table1(mid,name,msg) VALUES('aa','bb','cc')
--*能够改为其中的某字段

--oracle,mysql 适用方法

MERGE INTO table1 t 
USING (select 'aa' mid from dual ) t2
ON (t.mid = t2.mid) 
WHEN NOT MATCHED THEN 
    INSERT (mid,name,msg) VALUES('aa','bb','cc');

insert when exists(select * from table1 where mid = 'aa' ) into table1(mid,name,msg) select 'aa','bb','cc' from dual;
相关文章
相关标签/搜索