[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Insert where not exists

Posted on 2014-01-28
8
Medium Priority
?
511 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 1000 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this article, we’ll look at how to deploy ProxySQL.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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