Solved

Chained joins

Posted on 2004-10-11
7
1,092 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to send multiple emails at the same time in PHP 12 83
configure dependency in POM for new database 3 46
category table 2 36
SubQuery link 4 45
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
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…

726 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