Solved

MySQL: Select rows from one table that do not exist on another table

Posted on 2014-01-23
3
1,003 Views
Last Modified: 2014-01-23
I want to select all rows from table2 where table2 has a value in "isbn" that does not exist on table1.

This does not return anything:
SELECT * FROM table2 JOIN table1 on table2.isbn = table1.isbn WHERE table2.isbn != table1.isbn

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 4

Assisted Solution

by:ravikantninave
ravikantninave earned 400 total points
ID: 39804211
If the tables have compound primary keys, you'll have to use a "NOT EXISTS" clause

SELECT * 
  FROM TableA AS a 
 WHERE NOT EXISTS (
                   SELECT * 
                     FROM TableB b 
                    WHERE b.id1 = a.id1 
                          AND b.id2 = a.id2 
                          AND b.id3 = a.id3
                  );

Open in new window


or
SELECT TableA.* FROM TableA LEFT JOIN TableB ON TableA.id = TableB.id WHERE TableB.id IS NULL

Open in new window

0
 

Assisted Solution

by:Proud_Grandpa
Proud_Grandpa earned 100 total points
ID: 39804220
Would this work?

SELECT table2.* FROM table1, table2 WHERE table1.isbn NOT IN table2.isbn;
0
 
LVL 4

Accepted Solution

by:
ravikantninave earned 400 total points
ID: 39804233
This works perfectly:

SELECT Table_2.* FROM Table_2 LEFT JOIN Table_1 ON Table_2.isbn = Table_1.isbn WHERE Table_1.isbn  IS NULL
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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