From: | Andreas Kretschmer <akretschmer(at)spamfence(dot)net> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Tricky join question |
Date: | 2006-12-22 14:40:10 |
Message-ID: | 20061222144010.GA14630@KanotixBox |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Tim Tassonis <timtas(at)cubic(dot)ch> schrieb:
> This is absolutely not what I want. I want a row for every person and every
> course, regardless whether the person has taken the course or not. If the
> person has not taken the course, I want a null value in the person id
> column:
test=# select c.id, c.name, pc.person_id
from course c
cross join person p
left outer join person_course pc on (p.id,c.id)=(pc.person_id,pc.course_id);
id | name | person_id
----+--------------+-----------
1 | SQL Beginner | 1
1 | SQL Beginner |
1 | SQL Beginner | 3
2 | SQL Advanced |
2 | SQL Advanced |
2 | SQL Advanced | 3
(6 rows)
But i think, this is a little bit stupid, because row 4 and 5 are the
same.
Perhaps this would be better:
test=# select c.id, c.name, p.id as person, pc.person_id
from course c
cross join person p
left outer join person_course pc on (p.id,c.id)=(pc.person_id,pc.course_id);
id | name | person | person_id
----+--------------+--------+-----------
1 | SQL Beginner | 1 | 1
1 | SQL Beginner | 2 |
1 | SQL Beginner | 3 | 3
2 | SQL Advanced | 1 |
2 | SQL Advanced | 2 |
2 | SQL Advanced | 3 | 3
(6 rows)
Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknow)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
From | Date | Subject | |
---|---|---|---|
Next Message | Tim Tassonis | 2006-12-22 14:52:34 | Re: Tricky join question |
Previous Message | Tom Lane | 2006-12-22 14:28:26 | Re: Tricky join question |