Select & Insert across multiple databases with MySQL

泄露秘密 提交于 2019-12-09 05:10:46

问题


I have 2 identical tables in 2 different databases that reside on the same server. What would be the best way to copy data from table to another?


回答1:


Use:

INSERT INTO db1.table1
SELECT *
  FROM db2.table2 t2
 WHERE NOT EXISTS(SELECT NULL
                    FROM db1.table1 t1
                   WHERE t1.col = t2.col)

The exists is simplified, but you left out if there's a primary key/auto_increment to worry about/etc.




回答2:


Just to elaborate slightly on OMG Ponies' answer, you can use any WHERE clause, like so:

INSERT INTO db1.tablename
SELECT *
FROM db2.tablename src
WHERE src.lastlogin >= '2013-10-31 07:00:00'


来源:https://stackoverflow.com/questions/3399657/select-insert-across-multiple-databases-with-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!