Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

624 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