473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

LEFT JOIN Multiple Tables

jim
Hi,

I've browsed several posts, but still haven't found the answer I'm
looking for. I have one table (A) that contains a list of values I
want to return. I have two other tables (B) and (C) that may or may
not include the same values, but will have others I need to include.
I'd like to return all of the data from table A with a yes/no column
if table B has matching data and a third column of the same for table
C.

I can get one or the other, but I've not figured out how to do
multiple LEFT JOINs in Access. Any help would be greatly
appreciated. Here's a version of my query:

SELECT A.id
, A.date
, SWITCH(B.id <null, "0"
,B.id IS null,"1") AS B
, SWITCH(C.id <null, "0"
,C.id IS null,"1") AS C

FROM A

LEFT JOIN B
ON A.id = B.id

LEFT JOIN C
ON A.id = C.id
Thanks in advance to anyone with pointers.
Jim
;

Apr 24 '07 #1
5 32711
Jim, I'm a little confused, since you seem to be using B and C as table
names and also as field aliases.

You are also comparing a field to Null, which won't work. Need to test if it
Is Null.

Is this what you want:

SELECT A.id,
A.date,
(B.id Is Null) AS BidNull,
(C.id Is Null) AS CidNull
FROM (A LEFT JOIN B ON A.id = B.id)
LEFT JOIN C ON A.id = C.id;

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"jim" <ji*********@gmail.comwrote in message
news:11*********************@u32g2000prd.googlegro ups.com...
>
I've browsed several posts, but still haven't found the answer I'm
looking for. I have one table (A) that contains a list of values I
want to return. I have two other tables (B) and (C) that may or may
not include the same values, but will have others I need to include.
I'd like to return all of the data from table A with a yes/no column
if table B has matching data and a third column of the same for table
C.

I can get one or the other, but I've not figured out how to do
multiple LEFT JOINs in Access. Any help would be greatly
appreciated. Here's a version of my query:

SELECT A.id
, A.date
, SWITCH(B.id <null, "0"
,B.id IS null,"1") AS B
, SWITCH(C.id <null, "0"
,C.id IS null,"1") AS C

FROM A

LEFT JOIN B
ON A.id = B.id

LEFT JOIN C
ON A.id = C.id
Apr 25 '07 #2
On 24 Apr 2007 11:56:35 -0700, jim <ji*********@gmail.comwrote:

Rather than "<null" use "is not null".

-Tom.
>Hi,

I've browsed several posts, but still haven't found the answer I'm
looking for. I have one table (A) that contains a list of values I
want to return. I have two other tables (B) and (C) that may or may
not include the same values, but will have others I need to include.
I'd like to return all of the data from table A with a yes/no column
if table B has matching data and a third column of the same for table
C.

I can get one or the other, but I've not figured out how to do
multiple LEFT JOINs in Access. Any help would be greatly
appreciated. Here's a version of my query:

SELECT A.id
, A.date
, SWITCH(B.id <null, "0"
,B.id IS null,"1") AS B
, SWITCH(C.id <null, "0"
,C.id IS null,"1") AS C

FROM A

LEFT JOIN B
ON A.id = B.id

LEFT JOIN C
ON A.id = C.id
Thanks in advance to anyone with pointers.
Jim
;
Apr 25 '07 #3
jim
Thanks all. Let me add a little more detail.

I have a table with a list of ids (A). Another table (B), contains a
subset of those ids and table (C) a different subset. I ultimately
want to return the entire contents of table A with a fag in the
second column when the id matches with table B and a flag in the
third column when it matches with table C. When I LEFT JOIN either of
the tables as it's set up everything works just fine. When I try to
join *both* tables, however, the query fails to execute.

The alias is just a poor job of translating actual table names here
for illustrative purposes. The meaning should be the same.

Apr 26 '07 #4
For a tables tblA, with an AutoNumber primary key of id, and tblB, and tblC,
each with a LongInteger key of id, the following seems to work as you
describe... instead of calculating a positive 1 for the flag, I used a
True/False, which generated a -1 for the flag to indicate existence of the
record.
Apr 26 '07 #5
For a tables tblA, with an AutoNumber primary key of id, and tblB, and tblC,
each with a LongInteger key of id, the following seems to work as you
describe... instead of calculating a positive 1 for the flag, I used a
True/False, which generated a -1 for the flag to indicate existence of the
record. Here's the SQL for the Query, which I created in the Query Builder
and just copied SQL view:

SELECT tblA.id, IsNull([tblB].[id]) AS flgB, IsNull([tblC].[Id]) AS flgC
FROM (tblA LEFT JOIN tblB ON tblA.id = tblB.id) LEFT JOIN tblC ON tblA.id =
tblC.id;

Larry Linson
Microsoft Access MVP


"jim" <ji*********@gmail.comwrote in message
news:11*********************@u32g2000prd.googlegro ups.com...
Hi,

I've browsed several posts, but still haven't found the answer I'm
looking for. I have one table (A) that contains a list of values I
want to return. I have two other tables (B) and (C) that may or may
not include the same values, but will have others I need to include.
I'd like to return all of the data from table A with a yes/no column
if table B has matching data and a third column of the same for table
C.

I can get one or the other, but I've not figured out how to do
multiple LEFT JOINs in Access. Any help would be greatly
appreciated. Here's a version of my query:

SELECT A.id
, A.date
, SWITCH(B.id <null, "0"
,B.id IS null,"1") AS B
, SWITCH(C.id <null, "0"
,C.id IS null,"1") AS C

FROM A

LEFT JOIN B
ON A.id = B.id

LEFT JOIN C
ON A.id = C.id
Thanks in advance to anyone with pointers.
Jim
;

Apr 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: StealthBananaT | last post by:
My database has two tables - films has 10,000 records and reviews has 20,000 records. Whenever I try to list all the films and the count of its reviews, MySQL locks and I have to restart the...
0
by: Petre Agenbag | last post by:
Hi List Me again. I'm trying to return from multiple tables, the records that have field "information_sent" between two dates. The tables are all related by means of the id of the entry in the...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
2
by: Scott Snella | last post by:
Hi, I hope this is the right place to post, but, I am having a problem with an UPDATE command and a LEFT JOIN, I am using something like: UPDATE table_a LEFT JOIN table_b ON...
4
by: Marek Kotowski | last post by:
Hi, What is the real difference between LEFT JOIN and RIGHT JOIN? Are there situations where a query with RIGHT JOIN cannot be rewritten with LEFT JOIN and tables reversed? (and vice versa). ...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
1
by: Eitan M | last post by:
Hello, I want to do select like this : select t1.col_2 from table_1 t1, table_2 t2 where t1.col_1 = t2.col_1 (+) The above is correct syntax for Oracle. What is the correct syntax for...
9
by: shanevanle | last post by:
I have two tables that are pretty big. I need about 10 rows in the left table and the right table is filtered to 5 rows as well. It seems when I join the tables in the FROM clause, I have to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.