Re: joins?

From: "Gregory Wood" <gregw(at)com-stock(dot)com>
To: "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: joins?
Date: 2002-03-06 22:03:13
Message-ID: 005901c1c55b$24099c70$7889ffcc@comstock.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

  • Re: joins? at 2002-03-06 17:29:28 from David Eduardo Gomez Noguera

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Elphick 2002-03-06 22:53:33 Re: Problems with unconstrained join
Previous Message Darren Ferguson 2002-03-06 22:00:34 Re: move database directory