如何将 mysql 表附加到不同数据库中的另一个表
我想从一个数据库中获取一张表,并将该数据附加到另一个数据库中的表中。但是,它们具有相似的编号(包括 id),需要更新才能复制。有没有一个函数可以自动执行此操作?或者我需要在两者之间写一个脚本吗?
到目前为止我已经得到:
#!/bin/sh
mysqldump -uuser1 -ppw1 database1 table1 > /home/user/public_html/database1.sql --skip-add-drop-table --skip-create-options
mysql -uuser2 -ppw2 database2 < /home/user/public_html/database1.sql
rm /home/user/public_html/database1.sql
I would like to grab a table from one database and append this data to a table in another database. However, they have similar numbers (including the id) which need to be updated before they can be copied over. Is there a function available that could do this automatically? Or do I need to write a script in between?
So far I've got:
#!/bin/sh
mysqldump -uuser1 -ppw1 database1 table1 > /home/user/public_html/database1.sql --skip-add-drop-table --skip-create-options
mysql -uuser2 -ppw2 database2 < /home/user/public_html/database1.sql
rm /home/user/public_html/database1.sql
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从一个表中进行选择并将其插入到另一个表中。结果将“附加”到原始数据中。
将一个数据库中的表附加到另一数据库中的表
You could select from one table and insert it into another. The results will be "appended" to the original data.
To append a table from one database to a table from an other database
听起来通过脚本来做会更安全,这看起来很简单——只需从第一个数据库中获取数据并执行批量插入到另一个数据库中,让 mysql 自己处理 id。在任何下降脚本语言中,这应该需要大约 10-30 LOC,并且可以让您更好地控制结果。
Sounds like something that would be a lot safer to do via script, which seems simple enough - just grab the data from the first DB and perform batch inserts into the other, letting mysql handle the ids itself. This should take about 10-30 LOC in any descent scripting language, and gives you more control over the outcome.
我通过创建一个 php 脚本来解决这个问题,该脚本为每个新数据库创建新连接。该脚本首先清空主表,然后再追加其他表的数据。将第一个条目设置为 NULL 并让 $row[x] 从 1 开始,确保它附加。不知道这是否是最好的解决方案,但它有效。
I solved it by creating a php script that creates new connections for each new database. This script empties the main table first before it will append the data of the other tables. Having the first entry on NULL and having the $row[x] start on 1 makes sure it appends.. Don't know if it's the best solution, but it works.