CREATE TABLE `test_replaceinto` (
`compname` varchar(255) NOT NULL,
`text1` varchar(255) DEFAULT NULL,
`text2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`ids` bigint NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ids` DESC),
UNIQUE KEY `compname` (`compname`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
method 1
replace into test_replaceinto(compname,text1,text2)
select 11,11,11
UNION
select 11,22,233
唯一键compname相同会更新,但是自增主键ids会变化
method 2
INSERT INTO test_replaceinto(compname,text1,text2)
select 11,11,11
UNION
select 11,22,2333
ON DUPLICATE KEY UPDATE
text1 = VALUES(text1),
text2 = VALUES(text2);
唯一键compname相同会更新,并且自增主键ids不会变化,但是会影响下一条记录的主键ids的值,导致主键ids不是连续值
method 3 MySQL 在 8.0.19(8.0.22) 版本中引入了 MERGE INTO 语法
916

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



