Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

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 Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 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