Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy rows from one MySQL database to another

Tags:

sql

mysql

I have two tables in different databases (WAMP server) with the same structure. I want to copy from the first database (newkt) to the second one (oldkt) all rows that do not exist in the second database (oldkt).

newkt   -> table : users (1500 records) (id, name, password)
oldkt   -> table : users (1200 records) (id, name, password)

I want to actually add rows to the oldkt database whose id doesn’t exist in oldkt yet.

Also if I have more than 3 columns, can these be added automatically or I do have to tag all of them?

like image 752
KoZe Avatar asked Sep 13 '25 21:09

KoZe


1 Answers

You can do like the following:

    insert into database1.table  select * from database2.table where id not in(select id from database1.table);
like image 168
Atul Singh Rajpoot Avatar answered Sep 15 '25 10:09

Atul Singh Rajpoot