mysql使用php将特定列从一个数据库表复制到另一个数据库表

时间:2022-09-15 21:57:25

I'm trying to create a cron job through php that has mysql queries (NOT for backup purposes), and want it to copy two specific columns from a table from one database to another (the two databases are on the same server, but has different connections).

我正在尝试通过具有mysql查询的php创建一个cron作业(不用于备份目的),并希望它将一个表中的两个特定列从一个数据库复制到另一个数据库(这两个数据库在同一台服务器上,但是不同的联系)。

I've tried

我试过了

    insert into newDB.your_table select * from oldDB.your_table;

but that didn't work for some reason and i want it to be specific to 2 columns only.

但由于某种原因这不起作用,我希望它只针对2列。

Any help, code, example, tutorial would be much much appreciated.

任何帮助,代码,示例,教程将非常感谢。

Thank you for time.

谢谢你的时间。

1 个解决方案

#1


1  

As shown on this page http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

如本页所示http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

To selectively copy only specific columns from one table to another, the SQL:

要有选择地只将特定列从一个表复制到另一个表,SQL:

INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

So for your example: INSERT INTO newDB.some_table (id, value) SELECT id, value FROM oldDB.some_other_table;

因此,对于您的示例:INSERT INTO newDB.some_table(id,value)SELECT id,value FROM oldDB.some_other_table;

#1


1  

As shown on this page http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

如本页所示http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

To selectively copy only specific columns from one table to another, the SQL:

要有选择地只将特定列从一个表复制到另一个表,SQL:

INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

So for your example: INSERT INTO newDB.some_table (id, value) SELECT id, value FROM oldDB.some_other_table;

因此,对于您的示例:INSERT INTO newDB.some_table(id,value)SELECT id,value FROM oldDB.some_other_table;