0
votes

I have two mySql database db1 and db2. I need to copy the data from db1 to db2. Both the databases are under the same user. The table structure is different in both the database.

The columns structure are not the same in the tables.

Example

db1.Table1

-------------------------------------------------------
|  id  |  SessionID  |  Product1_Qty  |  Product2_qty |
-------------------------------------------------------  
|  1   |  12345      |       2        |       1       |
-------------------------------------------------------

db2.Table1 (Desired Output)

--------------------------------------------
|  id  |  SessionID  |  Product     |  Qty |
-------------------------------------------- 
|  1   |  12345      |  Product_1   |   2  |
--------------------------------------------
|  2   |  12345      |  Product_2   |   1  |
--------------------------------------------

Edit: For every sessionID (1 row) in db1.Table1 there should be 2 same sessionID (2 rows) in db2.Table1

I need the SQL script to copy data from db1.Table1 to db2.Table1.

What is the best way to achieve this?

3

3 Answers

0
votes

You can use below SQL to do this

MySQL> insert into db2.Table1 select * from db1.Table1;
0
votes

Try this

select * into databaseName.dbo.table1name from databaseName.dbo.table2name

Can you comment is this worked or not

0
votes

In case you have different number of columns you can use below query.

INSERT INTO db1.table1 (`column1`, `column2`)  (SELECT `column3`, `column3` FROM db2.table2)