user3429778
user3429778

Reputation: 35

mysql copying specific columns from one database table to another using php

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).

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.

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

Thank you for time.

Upvotes: 3

Views: 2039

Answers (1)

Redbeard011010
Redbeard011010

Reputation: 964

As shown on this page 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:

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;

Upvotes: 2

Related Questions