Mysql 将数据从一个数据库复制到另一个 WHERE

Mysql copy data from one database to another WHERE

我正在尝试将数据从一个数据库复制到另一个数据库。本质上,我有 2 个数据库。

所有客户都存储在 Customer 数据库中(1000 万条记录),但只有部分客户存储在 "Full Customer" 数据库中。 (500 万条记录)。

我想将所有客户记录复制到当前没有记录的完整客户数据库中。

我的伪代码用于编程,但我想知道我是否可以直接使用 mysql

for customer in customers:
    if customer.ID not in (select ID from fullcustomer)
        insert customer into fullcustomer

你可以,而且你应该直接使用MySQL。

试试这个。我假设 table 在同一个数据库中 - 如果不是,您应该在 table 名称前加上数据库名称。我还假设 full_customer table 中缺失的列已经存在(你已经把它放在那里了)。

基本意思是:"Insert into table full_customer records of users from table customer, that are not already there, using ID for comparison".

INSERT INTO full_customer (ID, Name, DOB, Address) (
   SELECT ID, Name, DOB, Address FROM customer c
   LEFT JOIN full_customer fc ON c.ID = fc.ID
   WHERE fc.ID IS NULL
)

你应该做的是事先做一个完整性检查,检查提供的 SQL 是否会 select 你真正想要插入的客户到 full_customer table:

SELECT ID, Name, DOB, Address FROM customer c
LEFT JOIN full_customer fc ON c.ID = fc.ID
WHERE fc.ID IS NULL

确保两个 table 中的 ID 列都已建立索引,否则将永远执行这些语句。