Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 29th, 2005, 06:18 AM
Authorized User
 
Join Date: Mar 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Opposite to INNER JOIN query

I have to tables, tbl1 and tbl2 which of them with an ID field. I need a query which return me in qryID1 row only the ID's from tbl1 without a correspondence in tbl2 and in qryID2 row only the ID's from tbl2 without correspondence in tbl1. Thank you!
 
Old April 29th, 2005, 06:50 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

An easy way to do this is to UNION two queries.

The two queries will be something like (I'm writing this on the fly so my syntax may not be exact):

Code:
SELECT * from tbl1 
LEFT JOIN tbl2 on tbl1.ID = tbl2.ID
WHERE tbl2.ID is Null

SELECT * from tbl2
LEFT JOIN tbl1 on tbl2.ID = tbl1.ID
WHERE tbl1.ID is null
To create a UNION query: create a query without any tables. Select the menu option Query | SQL Specific | Union.

The SQL is:

Code:
Select[listoffields] from qry1
UNION
Select [fieldsthatpairwith_listoffields] FROM qry2;
Of course you can roll that all into one query:

Code:
SELECT[listoffields] from tbl1 
LEFT JOIN tbl2 on tbl1.ID = tbl2.ID
WHERE tbl2.ID is Null
UNION
SELECT [fieldsthatpairwith_listoffields] from tbl2
LEFT JOIN tbl1 on tbl2.ID = tbl1.ID
WHERE tbl1.ID is null
Each SELECT statement in UNION must have the same number of fields as all other SELECT statements.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help using join in SQL query yogeshyl SQL Language 1 July 30th, 2008 04:12 AM
self join query error Aneri Visual Studio 2005 0 March 1st, 2007 05:35 AM
Nested Query or Join? rlull SQL Server 2000 4 November 10th, 2005 05:23 PM
JOIN query wverner Access 2 April 5th, 2005 01:44 PM
Join Query tsimsha Classic ASP Databases 2 December 2nd, 2004 09:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.