Solved

Chained joins

Posted on 2004-10-11
7
1,079 Views
Last Modified: 2012-08-13
Is it possible to chain a join to an outer joined table?
I have three tables, A, B, and C
Table A parent records have optional child records in table B.

So I did an outer join and it works fine.
But I want to expand a column of the child table B.

Parent Table A ---> left outer joined to optional child table B ----> lookup a column of B in table C

Can that be done ?




0
Comment
Question by:kaller2
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:AdrianSRU
ID: 12282241
You can have as many joins as you want.  It would look like this:

SELECT <columns> FROM (<table_A> LEFT JOIN <table_B> ON <join_condition>) LEFT JOIN <table_C> ON <join_condition;


--Adrian
0
 
LVL 2

Author Comment

by:kaller2
ID: 12282534
but does that join table A to C or B to C ?

I notice you used brackets around the FROM (), is that the trick ?

0
 
LVL 2

Author Comment

by:kaller2
ID: 12282571
I dont see any round brackets syntax used like that in the manual and it gaives an error when I try to use them like that.
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 12

Expert Comment

by:AdrianSRU
ID: 12283056
>but does that join table A to C or B to C ?

The join is performed based on the join conditions that are specified.  Here is an example:

TableA (
ID bigint not null primary key,
TableA_Field int)

TableB (
ID bigint not null primary key,
TableB_Field int)

TableC (
ID bigint not null primary key,
TabkeC_Field int)

This query joins TableA with TableB, and then joins TableB with TableC:

SELECT * FROM (TableA LEFT JOIN TableB ON TableA.TableA_Field=TableB.ID) LEFT JOIN TableC ON TableB.TableB_Field=TableC.ID


>I dont see any round brackets syntax used like that in the manual and it gaives an error when I try to use them like that.

The () around the join between TableA and TableB are not necessary, I just use them for readability.  Post the query that you are working with and I should be able to figure out what the problem is.


-Adrian
0
 
LVL 2

Author Comment

by:kaller2
ID: 12283515
#
# Table structure for table `A`
#

CREATE TABLE A (
  a tinyint(4) NOT NULL default '0',
  b tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (a,b)
) TYPE=MyISAM;

#
# Dumping data for table `A`
#

INSERT INTO A VALUES (100, 10);
INSERT INTO A VALUES (100, 11);
INSERT INTO A VALUES (100, 12);
# --------------------------------------------------------

#
# Table structure for table `B`
#

CREATE TABLE B (
  b tinyint(4) NOT NULL default '0',
  c tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (b)
) TYPE=MyISAM;

#
# Dumping data for table `B`
#

INSERT INTO B VALUES (10, 30);
INSERT INTO B VALUES (11, 30);
# --------------------------------------------------------

#
# Table structure for table `C`
#

CREATE TABLE C (
  c tinyint(4) NOT NULL default '0',
  d varchar(4) NOT NULL default '0',
  PRIMARY KEY  (c)
) TYPE=MyISAM;

#
# Dumping data for table `C`
#

INSERT INTO C VALUES (30, 'good');
INSERT INTO C VALUES (31, 'bad');

=========================================
The problem is to join A to B with an outer join,
and then chain the result to C

For example:
 
select A.b, B.c, C.d
from A,B,C
where A.b=B.b and B.c=C.c

gives
        
b       c       d
10       30       good
11       30       good

Which is not an outer join.
We need

b       c       d
10       30       good
11       30       good
12     NULL   NULL
0
 
LVL 12

Accepted Solution

by:
AdrianSRU earned 125 total points
ID: 12285927
When you list the tables in the FROM clause and handle the join conditions in the WHERE clause, you are essentially getting an INNER JOIN.  You need to use LEFT JOINs like this:

SELECT A.b, B.c, C.d FROM (A LEFT JOIN B ON A.b=B.b) LEFT JOIN C ON B.c=C.c


-Adrian
0
 
LVL 2

Author Comment

by:kaller2
ID: 12286593
Lesson learned: be careful with table order with asymmetric queries. The chained join will be ok as left join for my purposes.  Thanks for your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

23 Experts available now in Live!

Get 1:1 Help Now