如何使用php + mysql在两个数据库之间复制表?

时间:2014-06-11 03:13:11

标签: php mysql

如何使用php + mysql在两个数据库之间复制表?

有时我想在两个表之间直接使用php copy table,对我来说似乎太难了。有谁知道怎么做?请看下面的代码:

<?php 

// database: a   ,  table: user
name | age | sex
jimmy | 30 | male
ricky | 20 | male
tina | 20 | female

// database: b   ,  table: user
name | age
kelvin | 30

// I want to copy a.user to replace b.user completely, how to do it in php+mysql?
$db_a = $db->connect($a_config);
$db_b = $db->connect($b_config);
// $db_a->table('user')->copy_to($db_b); // only explaining what I want to do

// final result: b.user is
name | age | sex
jimmy | 30 | male
ricky | 20 | male
tina | 20 | female

1 个答案:

答案 0 :(得分:1)

create table db_a.table_name like db_b.table_name;

创建表后,您必须插入这样的数据

insert into db_b.table_name select * from db_a.table_name;