Solved

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

Posted on 2014-03-18
3
797 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 34

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now