批量插入数据
insert into t1 values(a,b),(a,b).....;
insert into select
INSERT INTO t
VALUES
(1, 20, 'a'),
(2, 26, 'b'),
(3, 30, 'c');
– 两张表的字段要一一对应
INSERT INTO t
SELECT id,age,name from t_copy where id < 10;
结果:
1 20 a
2 26 b
3 30 c
4 22 d
replace into
REPLACE INTO t (id,age) VALUES (2, 15);
结果:
1 20 a
2 15 NULL
3 30 c
4 22 d
insert into on duplicate key update
insert into on duplicate key update表示插入更新数据,当记录中有PrimaryKey,或者unique索引的话,如果数据库已经存在数据,则用新数据更新(update),如果没有数据效果则和insert into一样。
INSERT INTO t
(id, age)
VALUES
(3, 28),
(4, 29)
ON DUPLICATE KEY UPDATE
id = VALUES(id),
age = VALUES(age);
结果:
1 20 a
2 15 NULL
3 28 c
4 29 d
insert ignore into
insert ignore into表示尽可能的忽略冲突,暴力插入。
INSERT IGNORE INTO t VALUES(1,30,'f'),(6,33,'o');
结果:
1 20 a
2 15 NULL
3 28 c
4 29 d
6 33 o
来源:CSDN
作者:sunny david jin
链接:https://blog.csdn.net/qq_35936776/article/details/103835478