Tuesday, 14 February 2017

How to Insert data from one database table to another database table in Mysql

You may have feel the need to data migration from one database to another one both are the same local server, The tables and columns got different names and insert data from one database table to another database table using mysql query, Then following query will be very useful for doing same task.

Suppose you have 2 database cmp1, cmp2 and table name companies1, companies2 And your task is to insert data from second database table to first database table.
The tables columns are different.

First DB: cmp1.companies1

Name     Address     Email     Phone

Second DB: cmp2.companies2

Nm     Adds     email     Ph

INSERT INTO cmp1.companies (Name, Address, Email, Phone)
SELECT Nm, Adds, email, Ph FROM cmp2.companies2;


If you want to apply some condition on your query then you easily can.

INSERT INTO cmp1.companies (Name, Address, Email, Phone)
SELECT Nm, Adds, email, Ph FROM cmp2.companies2 WHERE cmp2.status = 1;

No comments:

Post a Comment

HTML APIs: What They Are And How To Design A Good One

As JavaScript developers, we regularly forget that not everybody has a similar data as USA. It’s referred to as the curse of knowledge:...