当前位置:网站首页>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 或者 空的 单引号
边栏推荐
- [matytype] insert MathType inter line and intra line formulas in CSDN blog
- 基金管理人的合规管理
- 程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
- Guys, how to export iceberg data to MySQL? What tools are there? Neither sqoop nor dataX
- Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
- Matplotlib data visualization Foundation
- CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
- 12 plateformes de gestion de produits utilisées par tout le monde
- 大佬们,数据湖iceberg的数据,怎样导出到mysql? 有什么工具? sqoop,datax都没
- 678. 有效的括号字符串
猜你喜欢

The exclusive collection of China lunar exploration project is limited to sale!

It is interesting to understand MMAP in this way!

Uncover the secrets of new products! Yadi Guanneng 3 multi product matrix to meet the travel needs of global users

数字藏品新一轮热度开启

CRC 校驗

The list of winners of the digital collection of "century master" was announced

NC | 肠道细胞和乳酸菌共同作用来防止念珠菌感染

Wireshark TS | 快速重传和乱序之混淆

bash: ln: command not found

Recommend a JSON visualization tool artifact!
随机推荐
Wireshark TS | 快速重传和乱序之混淆
Which securities company has a low, safe and reliable Commission for stock trading and account opening
基金管理人的内部控制
Can MySQL CDC take out the op field
【Laravel 】faker数据填充详解
Mobile hard drive reads but does not display drive letter
12 plateformes de gestion de produits utilisées par tout le monde
Crawler (2) - requests (1) | deep parsing of requests module
Project0: Games
Prefabricated dishes usher in the "golden age", who can lead the next trillion market
The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion
SQL server2014 failed to delete the database, with an error offset of 0x0000
Addition, deletion, modification and query of database
12款大家都在用的产品管理平台
php 实现抽奖功能
The exclusive collection of China lunar exploration project is limited to sale!
CRC 校验
Venv: directory structure of venv
Button button clear border
12款大家都在用的產品管理平臺