mysql 使用INSERT INTO....SELECt怎么一次性插入多行数据
如
INSERT INTO `user` (`id`, `name`, `age`)
SELECT null, ’张三‘, 12
FROM dual WHERe not exists
(select * from `user` where `user`.`name` = '张三');
我现在有多条数据,想批量插入该怎么办
--------------------------------
id | name | age
--------------------------------
1 张三 12
2 李四 13
3 王五 15
--------------------------------
已经解决了可以使用union all的方法
INSERT INTO `user` (`id`, `name`, `age`)
(SELECt null, ’张三‘, 12
FROM dual WHERe not exists
(select * from `user` where `user`.`name` = '张三') ) UNIOn ALL (SELECt null, ’李四‘, 13
FROM dual WHERe not exists
(select * from `user` where `user`.`name` = '李四') );
这样在批量插入的时候就不会有重复名字了
求助~~ INSERT INTO....SELECT怎么一次性插入多行数据
答案:2 悬赏:0
解决时间 2021-04-28 03:50
- 提问者网友:夜微涼
- 2021-04-28 00:54
最佳答案
- 二级知识专家网友:颜值超标
- 2021-04-28 01:01
INSERT INTO `user` (`id`, `name`, `age`)
SELECt null, `name`, `age`
FROM dual WHERe `name` != '张三';
SELECt null, `name`, `age`
FROM dual WHERe `name` != '张三';
全部回答
- 1楼网友:啵啵桃汀
- 2021-04-28 02:36
你可以把insert语句连起来写,两个insert语句中用半角的分号“;”隔开
insert into tab() values();insert into tab() values;……
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息