Solved

SQL search for records that do and do not exist in another table

Posted on 2014-03-18
3
812 Views
Last Modified: 2014-03-18
I have a fairly complex schema in Access 2007 that does a join on several tables based on primary / foreign keys.

The problem I am having is that queries I have created only contain records where both values exist in both tables....I need to include these, but also records where values don't exist in both tables.

For example:

Join Table A with Table B, on VesselID.

Table A contains: LocationID (PK), LocationName, VesselID(FK)
Table B contains: VesselID(PK), VesselName


I want to join Table A & B, but want to display all records in Table A that have a Vessel assigned to them, but also records that do not have a vessel assigned.

Right now, I'm doing an INNER JOIN, which shows all records in Table A that have references to Table B based on VesselID.  If I do a LEFT JOIN, I would get all records in Table A that do not have records in Table B, but I need to do both.

Am I missing something simple here?

My real query is much more complex, and involves several tables, but I'm trying to "dumb it down" here.

Any help would be appreciated.

Thanks...Tom
0
Comment
Question by:tpigielski
[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 7

Expert Comment

by:COACHMAN99
ID: 39937451
use a left join on the table you want all data from (regardless of right table)
e.g. SELECT Cust.Cust_Name, Tx.TX_Date FROM Cust LEFT JOIN Tx ON Cust.Cust_ID = Tx.Cust_ID;
0
 
LVL 38

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 39937484
When you use the QBE to build a query, it gives you a dialog with three choices when you draw a join line to connect two tables.  Read the options carefully and choose the one that fits your requirements.

Although the QBE can be awkward for complex criteria, I almost always use it to build the initial query and joins.  It saves a lot of typing.  Then I switch to SQL view if that is more convenient for entering the selection criteria.  

I rarely use SQL embedded in VBA but even when I do, I start with the QBE to make the query and test it.  Only after it is working do I switch to SQL view and copy and paste into VBA.

One thing I actively dislike about the QBE is that it rewrites your selection criteria so it can more easily display it in QBE view.  My solution for the queries where this is a problem is to always leave the querydef in SQL view once it is build.  As long as you never save it from QBE view, Access will not feel the need to mess up your carefully constructed criteria.
0
 
LVL 7

Accepted Solution

by:
COACHMAN99 earned 250 total points
ID: 39937489
Hence:
use a left join on the table you want all data from (regardless of right table)

e.g. select tblA.Location, tblB.VesselName etc from tblA Left Join tblB on tblA.VesselId = tblB.VesselId;
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

617 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