Solved

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

Posted on 2014-04-20
2
575 Views
Last Modified: 2014-04-20
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.
0
Comment
Question by:rleyba828
2 Comments
 
LVL 27

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 40011364
as long as the key exists and is defined on the combination of the 2 columns, you can just add the ignore keyword to your existing query and mysql will behave as expected (insert ignore into ...) without a where clause
0
 

Author Comment

by:rleyba828
ID: 40011405
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question