您现在的位置是:首页> 网站开发> MySql

mysql中insert ignore into,replace into,on duplicate key updat

  • 3033人已阅读
  • 时间:2018-12-29 10:32:39
  • 分类:MySql
  • 作者:祥哥

#用主键primary或者唯一索引unique区分了记录的唯一性,避免重复插入记录可以使用(如果数据存在则忽略)
INSERT IGNORE INTO `table_name` (`email`, `phone`, `user_id`) VALUES ('test9@163.com', '99999', '9999');

#用主键primary或者唯一索引unique区分了记录的唯一性,如果数据存在先删除在插入
REPLACE INTO `table_name` (`email`, `phone`, `user_id`) VALUES ('test569', '99999', '123');

#用主键primary或者唯一索引unique区分了记录的唯一性,如果重复就更新,否则就插入
语法:
INSERT INTO 表名(字段名1,字段名2,...) VALUES(值1,值2,...) ON DUPLICATE KEY UPDATE 字段名=值,...

例句:
INSERT INTO user(userId,userName,userAge,userSex) VALUES(1,"qyf",22,"nv") ON DUPLICATE KEY UPDATE userName="qinyufeng",userAge=24,userSex="女"

#mysql复制表
#完全复制
insert into table1 select * from tables2

#部分复制
insert into tab_a(id,  name, create_time, update_time, create_user,update_user) (select id,  name,create_time,update_time, create_user, update_user from tab_b);

#不复制重复纪录
insert into table1 select distinct * from table2

#复制前5条记录
insert into table1 select top 5 * from table2

#不在同一数据库完全复制
insert into db1.table1 select * from db2.table2


Top