简体   繁体   English

将特定记录从一个数据库导入到另一个数据库

[英]Import specific records from one database to another

I have 2 databases: 我有2个数据库:

DB1 DB1

customers (columns: customernr, name, adress, db2clientid) 客户(列:customernr,名称,地址,db2clientid)

invoices (columns: invoicenr, customernr, totalamount, taxamount, createdate) 发票(列:发票,客户,总金额,税额,已创建)

DB2 DB2

tblinvoices (columns: invoiceid, contactid, total, tax, date) tblinvoices(列:发票编号,联系方式,总计,税金,日期)

I want to import invoices from DB2 tblinvoices to invoices in DB1 using a cronjob. 我想使用cronjob将发票从DB2 tblinvoices导入到DB1中的发票。 I only want to import invoices from customers where the db1/customers/db2clientid = db2/tblinvoices/contactid . 我只想从db1/customers/db2clientid = db2/tblinvoices/contactid客户中导入发票。

I want to run it via a script (via MySQL), so I can create a cronjob that runs every hour. 我想通过脚本(通过MySQL)运行它,因此我可以创建一个每小时运行一次的cronjob。

My biggest concern is how to import only new records. 我最大的担心是如何仅导入新记录。 How can manage that not every cron run the complete table is imported, but only the records that aren't in DB1 yet? 如何管理不是所有运行完整表的cron都被导入,而是仅导入DB1中还没有的记录?

I can write simply SQL commands, but with this I have absolute no idea where to start looking. 我可以编写简单的SQL命令,但是我完全不知道从哪里开始寻找。

Can someone point in the right direction? 有人可以指出正确的方向吗? Which arguments to use? 使用哪些参数?

Provided you have same user credentials with similar previledges for both databases, you could follow the steps below. 如果两个数据库的user credentials similar previledges具有similar previledges ,则可以按照以下步骤操作。

Run the query1 to double check if it selects the correct records that needs to go into db1. 运行query1再次检查它是否选择了需要进入db1的正确记录。 Once confirm use the query2 below to insert these records. 确认后,使用下面的query2插入这些记录。

Query#1 查询#1

SELECT i.* 
        FROM db2.tblinvoices i 
        LEFT JOIN db1.invoices db1inv on db1inv.invoicenr=i.invoiceid 
        AND db1inv.contactid=i.customernr 
        WHERE db1.invoicenr IS NULL

Query#2 查询#2

 INSERT INTO db1.invoices (
    SELECT i.* 
    FROM db2.tblinvoices i 
    LEFT JOIN db1.invoices db1inv on db1inv.invoicenr=i.invoiceid 
    AND db1inv.contactid=i.customernr 
    WHERE db1.invoicenr IS NULL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM