1.这种写法无关主键
Insert or Ignore into tableName(col1,col2......) select '{0}','{1}'...
where not exists
(select col1,col2...... from tableName where col1='{0}' and col2='{1}' ....)
2.下面这种写法field1为主键
假设你的表叫:yourtable,其有二个字段:field1, field2(假设都是 int 型,field1 为主键)
如果不存在就插入,存在就忽略的方式,用 insert or ignore:
INSERT OR IGNORE INTO yourtable (field1,field2) VALUES(1,2)
或者如果不存在就插入,存在就更新的方式,用 insert or replace:
INSERT OR REPLACE INTO yourtable (field1,field2) VALUES(1,2);
本文详细介绍了在SQL中使用InsertorIgnore和InsertorReplace语句的两种不同场景。当需要根据主键判断是否插入或忽略记录时,可以使用InsertorIgnore;而当需要在存在记录时进行更新操作,则应选择InsertorReplace。这两种方法为数据库操作提供了灵活性。
1978

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



