使用 MySQL 跨多个数据库选择和插入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:45:34  来源:igfitidea点击:

Select & Insert across multiple databases with MySQL

sqlmysqldata-migration

提问by GSto

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?

我在驻留在同一台服务器上的 2 个不同数据库中有 2 个相同的表。将数据从表复制到另一个表的最佳方法是什么?

回答by OMG Ponies

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.

存在被简化了,但是如果有一个主键/auto_increment 需要担心/等,你就忽略了。

回答by Danny Beckett

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

只是为了稍微详细说明 OMG Ponies 的回答,您可以使用任何WHERE子句,如下所示:

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