Re: Conditional Join (or something along those lines)
| От | Joel Burton |
|---|---|
| Тема | Re: Conditional Join (or something along those lines) |
| Дата | |
| Msg-id | 004c01c21091$7e769c40$63eebed1@jburtonsat3005 обсуждение исходный текст |
| Ответ на | Conditional Join (or something along those lines) ("von Schwerdtner, Thomas (GXS, FPI)" <Thomas.vonSchwerdtner@gxs.ge.com>) |
| Список | 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 по дате отправления: