a select statement that sometimes joins

Поиск
Список
Период
Сортировка
От Mark Stosberg
Тема a select statement that sometimes joins
Дата
Msg-id Pine.BSF.4.33.0104101411440.82217-100000@nollie.summersault.com
обсуждение исходный текст
Ответы Re: a select statement that sometimes joins  ("Oliver Elphick" <olly@lfix.co.uk>)
Список pgsql-sql


Here's a situation I've run into more than once with SQL:

I want to select all the rows in a table that match a criteria, where one
of the criteria is possibly having a related entry in a second table. For
my example, lets say I have table named 'messages' and another named
'message_attachments'. The former has a primary key of msg_id, the latter
also contains msg_id, and has an attachment_id as it's primary key.

This statement shows me all the messages that also have attachments:

SELECTmessages.msg_id,message_attachments.attachment_idFROM messages,message_attachmentsWHERE messages.msg_id =
message_attachments.msg_id;

But I want a statement that says: "Show me all the messages, and include
information about an attachment if they have one"

(Let's further assume that a message will have only one attachment).

Is this possible? Anyone like to share an example? Much thanks.
 -mark

http://mark.stosberg.com/




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

Предыдущее
От: Joel Burton
Дата:
Сообщение: Re: Re: \i command
Следующее
От: Najm Hashmi
Дата:
Сообщение: Re: Re: \i command