Solved

t-sql look for records in two tables

Posted on 2016-11-07
2
49 Views
Last Modified: 2016-11-07
At this link
http://stackoverflow.com/questions/2686254/how-to-select-all-records-from-one-table-that-do-not-exist-in-another-table

I saw this example which selects all records from one table that do not exist in another table.
So it looks for the records in table t1 that do not exist in table t2

SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL


What's the syntax to look for records in two tables?
So look for the records in table t1 that do not exist in table t2 or table t3 ?

Anyone know?
0
Comment
Question by:maqskywalker
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41877987
SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t1.name = t2.name
LEFT JOIN table3 t3 ON t1.name = t3.name
WHERE t2.name IS NULL or t3.name IS NULL

Open in new window

This is sometimes referred to as a "Left Excluding JOIN"
see: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

Note if you only want rows in T1 that have no match in either t2 and t3 then change the OR to AND

SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t1.name = t2.name
LEFT JOIN table3 t3 ON t1.name = t3.name
WHERE t2.name IS NULL    and       t3.name IS NULL

Open in new window

1
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41877993
thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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