Solved

access query design not in join

Posted on 2014-11-05
9
207 Views
Last Modified: 2014-11-05
Can anyone provide a basic syntax, whereby you want to list all rows of data in one table that dont exist in another table. The tables are joined on a specific field. The query wizad has a few options but non that meet "not in" type queries. Any pointers?
0
Comment
Question by:pma111
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 47

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40423573
For those kind of queries I like to use the NOT EXIST clause:
SELECT *
FROM Table1
WHERE NOT EXISTS (SELECT 1 
              FROM Table2
              WHERE Table2.ID = Table1.ID)

Open in new window

0
 
LVL 3

Author Comment

by:pma111
ID: 40423590
What does the "SELECT 1" do in this context?
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40423597
Since you don't want to return nothing you can use any constant value (1, 2,..., 'a', 'T', ...) or even NULL if you want. Doesn't matter what you put there will work. It's only because the SELECT needs to have a least a column name or a value.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 3

Author Comment

by:pma111
ID: 40423603
getting a syntax error for some reason in the

WHERE NOT EXISTS (SELECT 1
              FROM Table2
              WHERE Table2.ID = Table1.ID)

section. This is access 2010.
0
 
LVL 3

Author Comment

by:pma111
ID: 40423661
it was because the field had a - in it so required the [ ] 's
0
 
LVL 5

Expert Comment

by:ReneD100
ID: 40423681
Problem with the 'where not in' and 'where not exists' is that Access handles those very slowly I've noticed. What I normally do is link the tables through the key (in this case the 'ID') and then right click on the join in the designder, select 'show all records from table1', and put a 'IS NULL' on the ID from the 2nd table.
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40423689
Shouldn't be slow if the ID has index on it.
What you did in the designer is something like this with SQL syntax:
SELECT Table1.*
FROM Table1
LEFT OUTER JOIN Table2 ON Table2.ID = Table1.ID
WHERE Table2.ID IS NULL

Open in new window

0
 
LVL 5

Expert Comment

by:ReneD100
ID: 40423700
I know what I did ;) But since the question was about 'the query wizard' I am sure it's easier to explain how to use it in the designer rather than the SQL editor.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40423810
using the Query wizard, select Find Unmatched Query wizard
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now