Solved

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

21 Experts available now in Live!

Get 1:1 Help Now