Solved

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

Posted on 2014-04-20
2
578 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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