Re: joins? - Mailing list pgsql-general

From Gregory Wood
Subject Re: joins?
Date
Msg-id 005901c1c55b$24099c70$7889ffcc@comstock.com
Whole thread Raw
In response to joins?  (David Eduardo Gomez Noguera <davidgn@servidor.unam.mx>)
Responses Re: joins?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
> i do joins on a foreign key.
> i.e.
> from (table1 left join table2 on table1.id=table2.fk_table1_id) left join
table3 on table1.id=table2.fk_table1_id...

You don't have to repeat the "ON" conditions from one join to the next.
Simply join tables to whatever tables have already been joined:

from (table1 left join table2 on table1.id=table2.fk_table1_id) left join
table3 on table2.id=table3.fk_table2_id...

> do you know if postgres takes any advantage of that? or if there is an
easier way to write it?
> (i.e. tell it to join on foreign keys constraints)

I'm not sure exactly what you mean by take advantage of it.

As far as an easier way to write it, there is no syntax to say "I want you
to join across foreign keys"*. Which is good, because you can have multiple
foreign keys referencing a table, which would really confuse things. You'll
just have to tell it what keys to join across.

* There is a way to perform a "natural" join, *if* both the primary and
foreign key have the same field name. I'm afraid I don't have time to
confirm the syntax (I never use this feature), but I believe it is:

table1 natural join table2 natural join table3

I'm not sure if you can combine natural and inner joins... perhaps someone
who uses natural joins could care to elaborate?

Greg


pgsql-general by date:

Previous
From: teg@redhat.com (Trond Eivind Glomsrød)
Date:
Subject: Re: Mandrake RPMs rebuilt
Next
From: Oliver Elphick
Date:
Subject: Re: Problems with unconstrained join