Solved

Insert where not exists

Posted on 2014-01-28
8
505 Views
Last Modified: 2014-01-30
Not sure of the syntax for this but I want to insert into one Table 1 selected columns from Table 2 where the unique identifier in Table 2 does not exist in Table 1
0
Comment
Question by:Gary
[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
  • 2
  • 2
  • +2
8 Comments
 
LVL 18

Assisted Solution

by:UnifiedIS
UnifiedIS earned 250 total points
ID: 39816033
Something like this should do it:

INSERT INTO Table1(ID)
SELECT ID
FROM table2
WHERE ID NOT IN (SELECT ID FROM Table1)
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 39816179
Building on UnifiedIS's solution, if you have a primary key on more than one column, then you can do it like this:

INSERT INTO Table1(cola, colb)
SELECT cola, colb
FROM table2
WHERE NOT EXISTS (SELECT 1 FROM Table1 WHERE cola = table2.cola and colb = table2.colb)

Open in new window

0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 39816232
Or just use IGNORE.

INSERT IGNORE INTO t1 (id) SELECT id FROM t2;

Open in new window

0
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.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 39816248
The IGNORE option will only work if there is a primary key or unique index on the table being inserted into that will be violated with the combination of columns used. eg if t1 had a primary key on cola, colb and colc (referring to my example above) then more rows may be inserted than intended, but if the primary key was on cola and colb, then it would work.
0
 
LVL 58

Author Comment

by:Gary
ID: 39816657
Just before I accept/split points I'd be interested into which of Terry's and Unified's solutions have less overhead.  From looking at it they both are minimising ignoring duplicates equally well. Maybe Terry's marginally as it only selecting unique row but probably doesn't matter that much in terms of memory.

As for the IGNORE solution, not efficient as it is trying to insert every row and I have over 5000 rows to go thru.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39817941
Try running an explain on each solution and look at the output to see which is likely to perform better.
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 39818839
It's true that the IGNORE method requires there to be a primary or unique key to collide with. But don't dismiss it as inefficient until you compare actual run times.
0
 
LVL 58

Author Closing Comment

by:Gary
ID: 39820899
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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