Solved

Insert where not exists

Posted on 2014-01-28
8
501 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
  • 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 31

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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 …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

12 Experts available now in Live!

Get 1:1 Help Now