当前位置:网站首页>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 或者 空的 单引号
边栏推荐
- 【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba
- What legal risks and qualifications should be paid attention to when building a digital collection platform?
- Submission lottery - light application server essay solicitation activity (may) award announcement
- 4hutool practice: dateutil format time [easy to understand]
- 建议收藏 | 在openGauss上遇到慢SQL该怎么办?
- Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
- Lack of comparator, operational amplifier to save the field! (the op amp is recorded as a comparator circuit)
- MySQL常用命令
- 基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建
- CRC 校验
猜你喜欢

数字藏品新一轮热度开启

106. 从中序与后序遍历序列构造二叉树

CRC 校验

Mobile hard drive reads but does not display drive letter

预制菜迎来“黄金时代”,谁能领跑下一个万亿市场

数字藏品平台搭建需要注意哪些法律风险及资质?

.NET 5.0+ 无需依赖第三方 原生实现定时任务

零基础入门测试该学什么?最全整理,照着学就对了

Simulink simulation circuit model of open loop buck buck buck chopper circuit based on MATLAB

What legal risks and qualifications should be paid attention to when building a digital collection platform?
随机推荐
新一代云原生数据库的设计与实践
Design and practice of new generation cloud native database
数据库的增删改查问题
678. 有效的括号字符串
CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
Error: missing revert data in call exception
What legal risks and qualifications should be paid attention to when building a digital collection platform?
[.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
数字藏品平台搭建需要注意哪些法律风险及资质?
华为HMS Core携手超图为三维GIS注入新动能
C [byte array] and [hexadecimal string] mutual conversion - codeplus series
内存泄漏定位工具之 valgrind 使用
华为HMS Core携手超图为三维GIS注入新动能
选择在中金证券上炒股开户可以吗?安全吗?
12款大家都在用的产品管理平台
基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建
新品大揭秘!雅迪冠能 3 多元产品矩阵,满足全球用户出行需求
I'd like to know where I can open an account in Guangzhou? Is it safe to open an account online now?
php 实现抽奖功能
In the new database era, don't just learn Oracle and MySQL