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
Solved

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

Posted on 2014-03-18
3
805 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
  • 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 35

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

809 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