Re: Conditional Join (or something along those lines) - Mailing list pgsql-novice

From Joel Burton
Subject Re: Conditional Join (or something along those lines)
Date
Msg-id 004c01c21091$7e769c40$63eebed1@jburtonsat3005
Whole thread Raw
In response to Conditional Join (or something along those lines)  ("von Schwerdtner, Thomas (GXS, FPI)" <Thomas.vonSchwerdtner@gxs.ge.com>)
List pgsql-novice
Your SQL:

SELECT
    r.region_name, r.region_no,
    cj.relationship,
    c.name_last,
    c.name_first,
    c.email
FROM
    ((region r
LEFT JOIN
    contact_join cj
ON
    ((cj.parent_id = r.region_id)))
LEFT JOIN
    contact c
ON
    ((c.id = cj.contact_id)))
WHERE
    (cj.parent_table = 'region');



The WHERE clause will get rid of all the work of your LEFT JOINS, since
parent_table won't = 'region' for the non-present halves, it will be null. A
kludgy solution would be to change the "WHERE parent_table = 'region'" to
"WHERE parent_table = 'region' OR parent_table IS NULL"; a much better
solution would be to change the ON clause of the first LEFT JOIN to
"parent_id = region_id AND parent_table = 'region'"

I haven't tested this -- in the future, if possible, it's nice to post a
small dump of your problem set (in addition to your clear explanation),
allowing list members to recreate your tables by just cutting & pasting the
DDL statements to test our advice.

- J.


pgsql-novice by date:

Previous
From: "von Schwerdtner, Thomas (GXS, FPI)"
Date:
Subject: Conditional Join (or something along those lines)
Next
From: stev knowles
Date:
Subject: no value in string