Link to home
Start Free TrialLog in
Avatar of rleyba828
rleyba828Flag for Australia

asked on

How to merge a mysql table into another without overwriting unique "key" fields.

Hi team,

  I just need some assistance merging some old SSL certificates data ("OldCerts" table)  into an SSL certificate master list  ("Cert_Inventory_Master_List" table) but without overwriting records where the "CN" field and the "Cert_Expiry_Date" of the "OldCerts" table already exists on the "Cert_Inventory_Master_List".   In other words, I need to treat "CN"+"Cert_Expiry_Date" as a unique "key".

I'm not sure how to formulate the SQL statement below:

INSERT INTO TABLE Cert_Inventory_Master_List (CN , Cert_Expiry_Date) 
SELECT (CN, Cert_Expiry_Date) from OldCerts where  ?????

Open in new window


For clarity, here's the scenario.

Cert_Inventory_Master_List   table (before)

PRI_KEY         CN                Cert_Expiry_Date
1       certificate.a.com          2016-12-24
2       certificate.b.com          2016-12-25
3       certificate.c.com          2016-12-26
4       certificate.d.com          2016-12-27



OldCerts table

PRI_KEY        CN                Cert_Expiry_Date
1       certificate.a.com          2016-12-24
2       certificate.b.com          2016-11-11
3       certificate.d.com          2016-12-26
4       certificate.e.com          2016-12-27


Cert_Inventory_Master_List (after merge)

PRI_KEY CN                      Cert_Expiry_Date
1       certificate.a.com          2016-12-24
2       certificate.b.com          2016-12-25
3       certificate.c.com          2016-12-26
4       certificate.d.com          2016-12-27
5       certificate.b.com          2016-11-11
6       certificate.d.com          2016-12-26
7       certificate.e.com          2016-12-27

Open in new window


* The idea is the two tables may have the same "CN" but if the expiry dates are different, then the merge should contain BOTH versions of the CN.

Thanks very much.
ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rleyba828

ASKER

Yes, that worked.   I created a unique key based on the two fields on the target table then did

INSERT IGNORE INTO  Cert_Inventory_Master_List (CN, Cert_Expiry_Date) 
SELECT CN, Cert_Expiry_Date FROM OldCerts;

Open in new window


Thanks very much.