PC Review


Reply
Thread Tools Rate Thread

How to join 4 tables with equi joins and left joins

 
 
=?Utf-8?B?bXNjZXJ0aWZpZWQ=?=
Guest
Posts: n/a
 
      29th Jun 2005
I need to join 4 tables R, S, C and X. I need an equi join between R and X
and a left join between R and S and R and C. None of my many SQL books give
the syntax for this. Anyone know the SQL syntax?

I have something that partially works but when the join column in table C is
null I get a row in the result set for every row in C instead of just the
null row.

Here is what I have right now (which does not use left joins):

SELECT R.ID, R.Title & "." & R.Chapter & "." & R.Article & "." & R.Topic &
iif(NZ(R.SubTopic,0)=0," ","." & R.SubTopic) AS RuleNumb, R.RuleName,
C.Category, S.Status
FROM tblRules AS R, tblPkgRuleXref AS X, tblCategories AS C, tblRuleStatuses
AS S
WHERE R.ID = X.RuleID and X.PkgID = [Forms]![frmPackages]![PkgID] AND
(C.ID = R.CategoryID OR R.CategoryID IS NULL) AND
(S.ID = R.StatusID OR R.StatusID IS NULL)
ORDER BY R.Title, R.Chapter, R.Article, R.Topic, R.SubTopic;
 
Reply With Quote
 
 
 
 
Gary Walter
Guest
Posts: n/a
 
      29th Jun 2005
Hi Rupert,

Maybe I misunderstand, but...

you can nest outer joins within an inner join

inner join:
FROM
(R INNER JOIN X
ON R.ID = X.RuleID)

nest first outer join in inner join:

FROM
(
(R INNER JOIN X
ON R.ID = X.RuleID)
LEFT JOIN S
ON R.CategoryID = S.ID)

nest both outer joins in inner join:

FROM
(
(
(R INNER JOIN X
ON R.ID = X.RuleID)
LEFT JOIN S
ON R.StatusID = S.ID)
LEFT JOIN C
ON R.CategoryID = C.ID)

(the 2 outer parentheses *may* not be needed)


SELECT .....
FROM
((tblRules R INNER JOIN tblPkgRuleXref X
ON R.ID = X.RuleID)
LEFT JOIN tblRuleStatuses S
ON R.StatusID = S.ID)
LEFT JOIN tblCategories C
ON R.CategoryID = C.ID
WHERE
X.PkgID = [Forms]![frmPackages]![PkgID]
ORDER BY
R.Title,
R.Chapter,
R.Article,
R.Topic,
R.SubTopic;


"mscertified" wrote:
>I need to join 4 tables R, S, C and X. I need an equi join between R and X
> and a left join between R and S and R and C. None of my many SQL books
> give
> the syntax for this. Anyone know the SQL syntax?
>
> I have something that partially works but when the join column in table C
> is
> null I get a row in the result set for every row in C instead of just the
> null row.
>
> Here is what I have right now (which does not use left joins):
>
> SELECT R.ID, R.Title & "." & R.Chapter & "." & R.Article & "." & R.Topic &
> iif(NZ(R.SubTopic,0)=0," ","." & R.SubTopic) AS RuleNumb, R.RuleName,
> C.Category, S.Status
> FROM tblRules AS R, tblPkgRuleXref AS X, tblCategories AS C,
> tblRuleStatuses
> AS S
> WHERE R.ID = X.RuleID and X.PkgID = [Forms]![frmPackages]![PkgID] AND
> (C.ID = R.CategoryID OR R.CategoryID IS NULL) AND
> (S.ID = R.StatusID OR R.StatusID IS NULL)
> ORDER BY R.Title, R.Chapter, R.Article, R.Topic, R.SubTopic;



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Equi join =?Utf-8?B?Q29hY2hCYXJrZXJPSlBX?= Microsoft Access Queries 4 12th Feb 2007 01:36 PM
Using a Non-Equi Join select records w search term Begin, Middle, =?Utf-8?B?UGF0cmljayBCcmlnZ3M=?= Microsoft Access Queries 6 11th Aug 2006 01:39 PM
Problem with multi-column equi-join query =?Utf-8?B?cGV0ZXIga2FwcGVz?= Microsoft Access Queries 5 1st Mar 2006 06:35 PM
Simple non-equi Join query Chris Microsoft Access Getting Started 3 12th May 2005 12:40 PM
Non Equi Join C. Houk Microsoft Access Queries 2 9th Apr 2004 07:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:46 AM.