Re: Select from multiple tables

Поиск
Список
Период
Сортировка
От Ernest E Vogelsinger
Тема Re: Select from multiple tables
Дата
Msg-id 5.1.1.6.2.20030606203424.040a8318@mail.vogelsinger.at
обсуждение исходный текст
Ответ на Select from multiple tables  (Jon Earle <je_pgsql@kronos.honk.org>)
Список pgsql-general
At 17:17 06.06.2003, Jon Earle said:
--------------------[snip]--------------------
>I want to select data from two tables, with the keying information for the
>second table coming from the select results of the first.  Can this be
>done in one call, or will I need to resort to two calls - one to get the
>record from the first table, then a second call to get the record from the
>second table based on a key contained in the first results set?
--------------------[snip]--------------------

Hint - get yourself a good book on SQL, or consult some online manuals.
What you want to do is called a JOIN:

SELECT table1.*, table2.* FROM table1
JOIN table2 ON table2.key = table1.foreignkey
WHERE table1.somcol = somevalue

This will give you all rows from table1 where a matching row in table2 exists.

SELECT table1.*, table2.* FROM table1
LEFT OUTER JOIN table2 ON table2.key = table1.foreignkey
WHERE table1.somcol = somevalue

This will give you all rows from table1 whether a matching row in table2
exists or not.

SELECT table1.*, table2.* FROM table1
RIGHT OUTER JOIN table2 ON table2.key = table1.foreignkey
WHERE table1.somcol = somevalue

This will give you all rows from table2 whether a matching row in table1
exists or not.




--
   >O     Ernest E. Vogelsinger
   (\)    ICQ #13394035
    ^     http://www.vogelsinger.at/



В списке pgsql-general по дате отправления:

Предыдущее
От: Richard Huxton
Дата:
Сообщение: Re: Select from multiple tables
Следующее
От: Jonathan Bartlett
Дата:
Сообщение: Re: Select from multiple tables