Link to home
Start Free TrialLog in
Avatar of weeezl
weeezl

asked on

Double Left Join?

I want to get values out of a group of tables but want to get null values in 2 instances if either join doesn't match on one table. I don't know the correct syntax in whic to write the query.

Here's the basic idea:


select au_key_content.input_content_id,au_key_content.key_id,au_row.row_id
from au_datatype,au_input_content,au_row
LEFT JOIN au_key_content on au_key_content.input_content_id=au_input_content.input_content_id
         AND au_key_content.row_id=au_row.row_id
WHERE au_row.content_id=#content# AND au_row.content_id=au_input_content.content_id
order by au_row.row_id

Of course this doesn't work because you can't write the join this way referencing two different databases in the same join statement. How would I write this to work so that au_key_content is joined conditionally on both tables? I want to get Null values for the keys if either join fails.
Avatar of weeezl
weeezl

ASKER

Sorry... one extra table reference

select au_key_content.input_content_id,au_key_content.key_id,au_row.row_id
from au_input_content,au_row
LEFT JOIN au_key_content on au_key_content.input_content_id=au_input_content.input_content_id
        AND au_key_content.row_id=au_row.row_id
WHERE au_row.content_id=#content# AND au_row.content_id=au_input_content.content_id
order by au_row.row_id
ASKER CERTIFIED SOLUTION
Avatar of ispaleny
ispaleny
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unsure about your db design, its either wrong formed, or redundantly joined in the query, or i dont understand your model...
anyway, i think that removing one of the wrongs 'join' should work...

Select
au_key_content.input_content_id,
au_key_content.key_id,
au_row.row_id

from au_input_content

LEFT JOIN au_row
     au_row.content_id=au_input_content.content_id

LEFT JOIN au_key_content on
       au_key_content.row_id=au_row.row_id

WHERE au_row.content_id=#content#
order by au_row.row_id


-----
Maybe i'm dumb and didnt get the meaning of the question... so also try this:

SELECT
(CASE au_key_content.input_content_id=au_input_content.input_content_id WHEN 1 THEN au_key_content.input_content_id
ELSE NULL END) AS input_content_id,

(CASE au_key_content.input_content_id=au_input_content.input_content_id WHEN 1 THEN au_key_content.key_id
ELSE NULL END) AS key_id,
au_row.row_id

from au_input_content

LEFT JOIN au_row
     au_row.content_id=au_input_content.content_id

LEFT JOIN au_key_content on
       au_key_content.row_id=au_row.row_id

WHERE au_row.content_id=#content#
order by au_row.row_id

good luck
Gerardo.-
Avatar of weeezl

ASKER

works fine when I use the full join syntax:

SELECT     au_row.row_id, au_input_content.input_content_id, au_key_content.key_id
FROM         au_input_content INNER JOIN
                      au_row ON au_input_content.content_id = au_row.content_id LEFT OUTER JOIN
                      au_key_content ON au_key_content.row_id = au_row.row_id AND au_input_content.input_content_id = au_key_content.input_content_id
WHERE     (au_input_content.content_id = #content#)
weeezl:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added to this question in more than 251 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: ispaleny http:#8095010

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

monosodiumg
EE Cleanup Volunteer