Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-23
3
Medium Priority
?
1,081 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 1600 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 400 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 1600 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 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