Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-18
3
Medium Priority
?
815 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 39

Assisted Solution

by:PatHartman
PatHartman earned 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
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…

722 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