简体   繁体   English

在mysql中将数据从一个数据库插入另一个数据库

[英]Insert data from one database to another in mysql

for a school project i have to insert data from a filled database into an empty database using a php script with sql, both databases are on the same server and both have the exact same structure, difference is that one is filled and the other is not. 对于一个学校项目,我必须使用带有sql的php脚本将填充的数据库中的数据插入到空数据库中,两个数据库都在同一服务器上,并且都具有完全相同的结构,不同之处在于一个数据库已填充,而另一个数据库未填充。

i made a start: 我开始了:

// get data from filled database
$select = $filledDb->prepare("SELECT * FROM artiekelen_inhoud, artikelen, gebruikers, rollen");
$select->execute();

// insert data into empty database
$insert = $emptyDb->prepare("INSERT INTO ");

connection to db is already set up and that works fine. 与数据库的连接已经建立,并且可以正常工作。

i'm not entirely sure how i should do this, should i insert all the rows manually or is there someway to insert all data with one query or something? 我不完全确定应该怎么做,应该手动插入所有行还是以某种查询或某种方式插入所有数据?

thank you for reading! 感谢您的阅读!

You can use INSERT ... SELECT for this. 您可以为此使用INSERT ... SELECT In short you just do: 简而言之,您只需:

INSERT INTO dba.blanktable
 SELECT * FROM dbb.yourothertable;

This assume that both tables exist, target table is empty and both have somewhat similar column types. 假设两个表都存在,目标表为空,并且都具有一些相似的列类型。

您可以通过运行以下命令在一个查询中执行SELECT and the INSERT

INSERT INTO db1.table1 SELECT * FROM db2.table1;
INSERT INTO destination_database.destination_table
SELECT * 
FROM source_database.source_table
INSERT INTO NEWDB.TABLE_NAME SELECT * FROM SOURSE_DATABASE.TABLE_NAME;

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

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