当前位置:网站首页>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 或者 空的 单引号
边栏推荐
- Which securities company has a low, safe and reliable Commission for stock trading and account opening
- Dotnet console uses microsoft Maui. Getting started with graphics and skia
- Recommend a JSON visualization tool artifact!
- 12. Gateway new generation gateway
- 2022年现在在网上开通股票账户安全吗?会不会有什么危险?
- CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
- 北汽蓝谷:业绩承压,极狐难期
- About database: how to avoid deadlock in gbase 8s
- 关于#SQL#的问题,如何解决?
- New situation of digital collection market
猜你喜欢

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

SQL SERVER2014删除数据库失败,报错偏移量0x0000...

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

bash: ln: command not found

Mobile hard drive reads but does not display drive letter

【Matytype】在CSDN博客中插入Mathtype行间与行内公式

The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion

Prefabricated dishes usher in the "golden age", who can lead the next trillion market
![[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul

数字藏品平台搭建需要注意哪些法律风险及资质?
随机推荐
[.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
【MPC】①二次规划问题MATLAB求解器quadprog
What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
《百年巨匠》数字藏品中奖名单公布
Have the bosses ever done the operation of sink shunting and writing to Clickhouse or other databases.
IDEA运行报错Command line is too long. Shorten command line for...
机器学习之线性回归详解
JS基础--数据类型
华为HMS Core携手超图为三维GIS注入新动能
MySQL common commands
bash: ln: command not found
Continue to advance, and softcom power steadily promotes cloud intelligence strategy
Prefabricated dishes usher in the "golden age", who can lead the next trillion market
Valgrind usage of memory leak locating tool
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
It is interesting to understand MMAP in this way!
CRC verification
Is it safe to open a stock account online in 2022? Is there any danger?
零基础入门测试该学什么?最全整理,照着学就对了
Button button clear border