Solved

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

Posted on 2014-04-20
2
571 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 26

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now