当前位置:网站首页>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 或者 空的 单引号
边栏推荐
- 投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
- Wireshark TS | confusion between fast retransmission and out of sequence
- 2022年现在在网上开通股票账户安全吗?会不会有什么危险?
- Sqlachemy common operations
- Does anyone know the logic of limit statement execution in Clickhouse? In the picture, the SQL above can be executed successfully
- [.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
- 基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建
- CCNP Part XII BGP (IV)
- Recommend a JSON visualization tool artifact!
- C [byte array] and [hexadecimal string] mutual conversion - codeplus series
猜你喜欢

CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)

北汽蓝谷:业绩承压,极狐难期

关于#SQL#的问题,如何解决?

Kotlin 协程调度切换线程是时候解开真相了

C one line code calculates the MD5 value of the file - codeplus series

新一代云原生数据库的设计与实践

A new round of popularity of digital collections opens

【邂逅Django】——(二)数据库配置

12款大家都在用的产品管理平台

基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建
随机推荐
12. Gateway new generation gateway
爬虫(2) - Requests(1) | Requests模块的深度解析
106. 从中序与后序遍历序列构造二叉树
Is the securities account opened by Yixue school for individuals safe? Is there a routine
数字藏品平台搭建需要注意哪些法律风险及资质?
Daily mathematics serial 55: February 24
建议收藏 | 在openGauss上遇到慢SQL该怎么办?
零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)
大佬们 有没有搞过sink分流写入clickhouse 或者其他数据库的操作。
dotnet 控制台 使用 Microsoft.Maui.Graphics 配合 Skia 进行绘图入门
What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
SQLAchemy 常用操作
How to solve the problem of SQL?
SQL server2014 failed to delete the database, with an error offset of 0x0000
基金管理人的内部控制
Wireshark TS | confusion between fast retransmission and out of sequence
数字藏品新一轮热度开启
CRC 校驗
Suggest collecting | what to do when encountering slow SQL on opengauss?
Prism journal navigation button usability exploration record