繁体   English   中英

MySQL更新表列从一个数据库到另一个

[英]Mysql update table column from one database to another

我正在研究一个脚本,该脚本的更新来源为:

tenesig_zink_production | euid0_hikashop_product | product_quantity

至:

tenesig_zink_dev | euid0_hikashop_product | product_quantity

我必须这样做,因为我们的库存编号存储在生产实例上。 因此,在从我们的开发人员实例推送新版本之前,我必须在推送之前从生产中更新开发人员的库存。 我对mySQL感到非常生锈,到目前为止,这是我需要知道的正确查询内容。

<?php
$host1="localhost"; // destination
$base1="tendesig_zink_dev";
$user1="tendesig_zink";
$password1="1,&#GZAWiB5z";

$host2="localhost"; //source
$base2="tendesig_zink_production";
$user2="tendesig_zink";
$password2="1,&#GZAWiB5z";


$conection1 = @mysql_connect($host1, $user1, $password1) 
or die("Error reaching destination<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 1! <br>";

$Db1 = @mysql_select_db($base1, $conection1)
or die("Error reaching destination database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database1 reached!<br>"; 

$conection2 = @mysql_connect($host2, $user2, $password2) 
or die("Error reaching source<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 2!<br>";

$Db2 = @mysql_select_db($base2, $conection2)
or die("Error reaching source database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database2 reached!!<br>"; 



$query = 'create table destination_table select * from second_database.source_table'; 
//echo "<br>".$query."<br>"; 
$result2 = mysql_query($query2, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());



mysql_close($conection1);
mysql_close($conection2);
?>

您的查询是错误的。 您想做这样的事情:

CREATE TABLE destination_database.destination_table LIKE source_database.source_table

然后运行:

INSERT INTO destination_database.destination_table 
SELECT * FROM source_database.source_table

在(未经测试的)PHP代码中:

$create_query = 'CREATE TABLE destination_database.destination_table LIKE source_database.source_table'; 
$result = mysql_query($create_query, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());

$insert_query = 'INSERT INTO destination_database.destination_table SELECT * FROM source_database.source_table'; 
$result = mysql_query($insert_query , $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM