我有一个场景,我需要通过检查记录是否存在来插入多个记录。每次启动服务器时,它都会运行sql script
( .sql文件),其中包含truncate table
语句,以便删除之前的行。是否有一种方法可以在运行脚本时不插入以前的记录而不使用脚本中的truncate
语句
INSERT INTO employeedetails (EmpNo, FirstName, LastName,DateOfBirth, EmailId, PANNo, PFNumber, BankName, JoiningDate,
Designation, Location, LOPDays, EmployeeStatus) VALUES
(1,'A','S','1965-04-14','m@nth.com','ADJP','KN/43708/01','XYZ','2007-10-15','Director','PQR','NIL',1),
(2,'B','N','1971-01-23','r@nth.com','AAGP','KN/43708/02','XYZ','2007-11-28','Director','PQR','NIL',1),
(3,'C','S','1982-10-05','d@nth.com','AIBP','KN/43708/03','XYZ','2008-03-17','SA','PQR','NIL',1),
(7,'D','M','1978-02-17','p@nth.com','AHRP','KN/43708/07','XYZ','2008-07-14','C T O','PQR','NIL',1),
(8,'E','D','1983-05-12','s@nth.com','BUMP','KN/43708/08','XYZ','2008-05-05','TL','PQR','NIL',1);
答案 0 :(得分:1)
您可以尝试使用 INSERT IGNORE
INSERT IGNORE INTO employeedetails(EmpNo,FirstName, LastName,DateOfBirth,EmailId,PANNo,PFNumber,BankName,JoiningDate, 名称,位置,LOPDays,EmployeeStatus)价值观 (1, 'A', 'S', '1965年4月14日', 'm@nth.com', 'ADJP', 'KN / 43708/01', 'XYZ', '2007-10-15' , '导演', '焊接工艺评定', 'NIL',1) (2, 'B', 'N', '1971年1月23日', 'r@nth.com', 'AAGP', 'KN / 43708/02', 'XYZ', '2007-11-28' , '导演', '焊接工艺评定', 'NIL',1) (3, 'C', 'S', '1982年10月5日', 'd@nth.com', 'AIBP', 'KN / 43708/03', 'XYZ', '2008-03-17' , 'SA', 'PQR', 'NIL',1), (7, 'd', 'M', '1978年2月17日', 'p@nth.com', 'AHRP', 'KN / 43708/07', 'XYZ', '2008-07-14' ,'C T O','PQR','NIL',1), (8, 'E', 'd', '1983年5月12日', 's@nth.com', 'BUMP', 'KN / 43708/08', 'XYZ', '2008-05-05' 'TL', 'PQR', 'NIL',1);
编辑:将H2 database
更新为最新版本并使用
ON DUPLICATE KEY UPDATE 。示例代码:
INSERT INTO employeedetails (EmpNo, FirstName, LastName,DateOfBirth, EmailId, PANNo, PFNumber, BankName,
JoiningDate,Designation, Location, LOPDays, EmployeeStatus, on_duplicate_update_count) VALUES
(1,'A','S','1965-04-14','m@nth.com','ADJP','KN/43708/01','XYZ','2007-10-15','Director','PQR','NIL',1,0),
(2,'B','N','1971-01-23','r@nth.com','AAGP','KN/43708/02','XYZ','2007-11-28','Director','PQR','NIL',1,0),
(3,'C','S','1982-10-05','d@nth.com','AIBP','KN/43708/03','XYZ','2008-03-17','SA','PQR','NIL',1,0),
(7,'D','M','1978-02-17','p@nth.com','AHRP','KN/43708/07','XYZ','2008-07-14','C T O','PQR','NIL',1,0),
(8,'E','D','1983-05-12','s@nth.com','BUMP','KN/43708/08','XYZ','2008-05-05','TL','PQR','NIL',1,0)
ON DUPLICATE KEY UPDATE on_duplicate_update_count=on_duplicate_update_count+1;
http://dev.mysql.com/doc/refman/5.6/en/insert.html
或
INSERT ... ON DUPLICATE KEY UPDATE
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html