Copy from table to table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Copy from table to table

Post by mccommunity »

I need to build a utility that will copy one table to another and reorder it to go into the correct columns on the second table:

ex: table1.column1 ->goes to table2.column13
table1.column2 -> table2.column9
table1.column3 -> table2.column1

and so on......


Is there a function or a tutorial on a way to do this? I have done a copy table to tablenew but this is not the same. Thanks.



mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

mysql> select * from tst1;
+------+------+
| id   | bid  |
+------+------+
|    1 | as   |
|    3 | qa   |
|    6 | lu   |
|    7 | hu   |
|   34 | bra  |
+------+------+
5 rows in set (0.01 sec)

mysql> select * from tst2;
Empty set (0.00 sec)

mysql> insert into tst2 select bid as qud, id as lid from tst1;
Query OK, 5 rows affected (0.03 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from tst2;
+------+------+
| qud  | lid  |
+------+------+
| as   |    1 |
| qa   |    3 |
| lu   |    6 |
| hu   |    7 |
| bra  |   34 |
+------+------+
5 rows in set (0.00 sec)

mysql>
Is that is what you're looking for?
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Yes... thank you

Post by mccommunity »

Yes this is it, thanks.
Post Reply