maxdachs
maxdachs

Reputation: 59

How to select from one database and insert it into another?

I have 2 MySQL databases with 1 table in each one.

Table1 (name,steamid,time) Table2 (name,steamid)

Now I want to select all data from Table1, where the Time (in sec.) is for example more than 500. And then I want to insert the name and the steamid into the Table 2, if the steamid does not exist in Table2.

How is this possible? The tables are in 2 databases and not in the same one.

Upvotes: 0

Views: 2368

Answers (2)

Sapnix
Sapnix

Reputation: 55

As of Mysql 5.7 syntax slightly changed

INSERT INTO db1.Tbl1(field1,field2) SELECT field1, field2 FROM db2.Tbl2

Source: Mysql doc

Upvotes: 0

Fahim
Fahim

Reputation: 12358

Try like this

insert into db1.Tbl1(name,steamid) values (select name, steamid from db2.Tbl2)

Upvotes: 1

Related Questions