Solved

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

Posted on 2014-01-23
3
1,023 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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