Reputation: 526
I have 2 databases:
DB1
customers (columns: customernr, name, adress, db2clientid)
invoices (columns: invoicenr, customernr, totalamount, taxamount, createdate)
DB2
tblinvoices (columns: invoiceid, contactid, total, tax, date)
I want to import invoices from DB2 tblinvoices
to invoices in DB1 using a cronjob. I only want to import invoices from customers where the 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.
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?
I can write simply SQL commands, but with this I have absolute no idea where to start looking.
Can someone point in the right direction? Which arguments to use?
Upvotes: 1
Views: 107
Reputation: 3543
Provided you have same user credentials
with similar previledges
for both databases, you could follow the steps below.
Run the query1 to double check if it selects the correct records that needs to go into db1. Once confirm use the query2 below to insert these records.
Query#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
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)
Upvotes: 1