Re: joins?

Поиск
Список
Период
Сортировка
От Gregory Wood
Тема Re: joins?
Дата
Msg-id 005901c1c55b$24099c70$7889ffcc@comstock.com
обсуждение исходный текст
Ответ на joins?  (David Eduardo Gomez Noguera <davidgn@servidor.unam.mx>)
Ответы Re: joins?  (Tom Lane <tgl@sss.pgh.pa.us>)
Список 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 по дате отправления:

Предыдущее
От: teg@redhat.com (Trond Eivind Glomsrød)
Дата:
Сообщение: Re: Mandrake RPMs rebuilt
Следующее
От: Oliver Elphick
Дата:
Сообщение: Re: Problems with unconstrained join