Solved

Chained joins

Posted on 2004-10-11
7
1,085 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

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.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

919 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

14 Experts available now in Live!

Get 1:1 Help Now