开发者

How to append a mysql table to another table in a different database

开发者 https://www.devze.com 2023-03-27 04:12 出处:网络
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 co

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
mysqld开发者_运维百科ump -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


You could select from one table and insert it into another. The results will be "appended" to the original data.

insert into new_table (id, name) select old_id, old_name from old_table;

To append a table from one database to a table from an other database

insert into new_database.new_table (id, name) select old_id, old_name from old_database.old_table; 


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.


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.

<?php

$db_user = "database_all_usr";
$db_pw = "";
$db_database = "database_all_db";

mysql_connect("localhost", $db_user, $db_pw) or die(mysql_error());
mysql_select_db($db_database) or die(mysql_error());

$sql = "TRUNCATE TABLE table_all";
mysql_query($sql) or die(mysql_error());

copy_table("database1_db","database1_usr","",$db_database,$db_user,$db_pw);
copy_table("database2_db","database2_usr","",$db_database,$db_user,$db_pw);

function copy_table($db_current,$db_user_current,$db_pw_current,$db_host,$db_user_host,$db_pw_host){

    mysql_connect("localhost", $db_user_current, $db_pw_current) or die(mysql_error());
    mysql_select_db($db_current) or die(mysql_error()); 

    $sql = "SELECT * FROM table";

    $result = mysql_query($sql) or die(mysql_error());

    mysql_connect("localhost", $db_user_host, $db_pw_host) or die(mysql_error());
    mysql_select_db($db_host) or die(mysql_error());

    while ($row = mysql_fetch_row($result)) {

        $sql = "INSERT INTO table_all VALUES (NULL, '$row[1]', '$row[2]')"; //adapt to the amount of columns
        mysql_query($sql) or die(mysql_error());
    }   
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消