当前位置:网站首页>mysql如何把 一个数据库中的表数据 复制到 另一个数据库中(两个数据库不在同一个数据库链接下)
mysql如何把 一个数据库中的表数据 复制到 另一个数据库中(两个数据库不在同一个数据库链接下)
2022-07-01 10:42:00 【12程序猿】
目录
1.需求:
把 一个数据库连接(如:test)中某个数据库中的某表(如:warn_message)的数据,批量复制到 另一个数据库连接(如:localhost)中某个数据库中的某表(如:warn_message)中
两个warn_message表的数据结构一致
2.实现
mysql 批量添加语句:
insert into 表名(字段1,字段2,字段3)values (字段1的值,字段1的值,字段1的值),(字段1的值,字段1的值,字段1的值);
下面的步骤就是要 组装 values 后面的 值,以达到可以批量复制的效果:
2.1 把待复制的数据 复制粘贴到 excel中


2.2 处理excel中的数据
然后根据需求修改excel中的数据,如 删除自增主键id,时间字段等特殊格式的修改的
把excel 组装成 可以批量 添加的参数
①删除id 所在列
②中文内容、时间列 两端 添加 单引号
选中中文所在的列,右键—》设置单元格格式—》选中自定义,选中如下类型,在类型中添加 单引号,点击确定:

时间所在列 处理方式相同:


③每行的首尾 添加 括号


④空值要处理成 null 或者 空的单引号
如果不处理,会报错,见下面。
⑤文件另存为 *.csv文件

在弹出的窗口中选择 是:

⑥以记事本方式 打开上面的 333.csv文件


发现记事本中的内容 离 目标 还差点, 多了 双引号!!!
解决方法:全局替换 “”,得到下面的内容:
2.3 把记事本的内容复制到 mysql 客户端工具,拼接 批量插入语句

执行报错:说明 空值也要进行处理。 处理成 null 或者空字符串
处理后的sql语句是:
INSERT INTO `warn_message`(`device_id`, `device_type`, `extend_json`, `install_place_id`, `pound_order_id`, `rational_deviation`, `real_deviation`, `overtime_duration`, `warn_type`, `deal_flag`, `deal_result`, `active_flag`, `created_user`, `created_time`, `updated_user`, `updated_time`, `warning_level`, `organization_id`, `start_time`, `end_time`, `description`, `resolver`, `third_warn_id`, `relation_user_id`) VALUES
(1842,8,null,102,0,0,0,0,0,1,'系统自动解除预警',1,0,'2021/2/3 11:23',0,'2021/2/3 14:40',3,1047,'2021/2/3 11:23','2021/2/3 14:40',null,null,6534,null),
(1842,8,null,102,0,0,0,0,0,1,'系统自动解除预警',1,0,'2021/2/3 11:43',0,'2021/2/3 14:40',3,1047,'2021/2/3 11:43','2021/2/3 14:40',null,null,6534,null),
(1842,8,null,102,0,0,0,0,0,0,'系统自动解除预警',1,0,'2021/2/3 12:11',0,'2021/2/3 13:56',3,1047,'2021/2/3 12:11','2021/2/3 13:56',null,null,6534,null),
(1842,8,null,102,0,0,0,0,8,1,'系统自动解除预警',1,0,'2021/2/3 13:56',0,'2021/9/30 11:48',3,1047,'2021/2/3 13:56','2021/2/3 14:40','烟火',null,6534,null),
(1842,8,null,102,0,0,0,0,8,1,'系统自动解除预警',1,0,'2021/2/3 13:59',0,'2021/9/30 11:48',3,1047,'2021/2/3 13:59','2021/2/3 14:40','烟火',null,6534,null),
(1842,8,null,102,0,0,0,0,8,1,'系统自动解除预警',1,0,'2021/2/3 14:06',0,'2021/9/30 11:48',3,1047,'2021/2/3 14:06','2021/2/3 14:40','烟火',null,6534,null),
(1842,8,null,102,0,0,0,0,8,1,'系统自动解除预警',1,0,'2021/2/3 14:10',0,'2021/9/30 11:48',3,1047,'2021/2/3 14:10','2021/2/3 14:40','烟火',null,6534,null),
(1842,8,null,102,0,0,0,0,8,0,'系统自动解除预警',1,0,'2021/2/3 14:40',0,'2021/9/30 11:48',3,1047,'2021/2/3 14:40',null,'烟火',null,6534,null)
运行结果如下:

批量插入成功!!
2.4 注意点:excel中空值也要处理
excel中的数据 处理需要注意的点:
①中文和时间 都要加 单引号
②空值也要处理 处理成 null 或者 空的 单引号
边栏推荐
- PHP realizes lottery function
- 数据库的增删改查问题
- SQL server2014 failed to delete the database, with an error offset of 0x0000
- Uncover the secrets of new products! Yadi Guanneng 3 multi product matrix to meet the travel needs of global users
- The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion
- 选择在中金证券上炒股开户可以吗?安全吗?
- SQL SERVER2014删除数据库失败,报错偏移量0x0000...
- CRC 校驗
- SQLAchemy 常用操作
- Addition, deletion, modification and query of database
猜你喜欢

SQL server2014 failed to delete the database, with an error offset of 0x0000

Common penetration tools -goby

新品大揭秘!雅迪冠能 3 多元产品矩阵,满足全球用户出行需求

CRC check

Venv: directory structure of venv

What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it

缺少比较器,运放来救场!(运放当做比较器电路记录)
![[matytype] insert MathType inter line and intra line formulas in CSDN blog](/img/ff/871a3f06f898ed107a2a974d2c7bc4.png)
[matytype] insert MathType inter line and intra line formulas in CSDN blog

CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
![[MPC] ① quadratic programming problem matlab solver quadprog](/img/be/5e300255041e3348b933bc32e2ea46.png)
[MPC] ① quadratic programming problem matlab solver quadprog
随机推荐
关于#SQL#的问题,如何解决?
新一代云原生数据库的设计与实践
What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
CRC 校驗
Japanese professor sues Intel FPGA and SOC products for infringing a design patent
12款大家都在用的产品管理平台
Valgrind usage of memory leak locating tool
数字藏品市场新局面
数据库的增删改查问题
Write your own who commands
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
SQL Server列一相同的情况下,如何取列二的最大值,并重新生成表
《百年巨匠》数字藏品中奖名单公布
Daily mathematics serial 55: February 24
mysql cdc能把能把op字段拿出来吗
12 plateformes de gestion de produits utilisées par tout le monde
毕业季·进击的技术er
venv: venv 的目录结构
. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks