a select statement that sometimes joins - Mailing list pgsql-sql

From Mark Stosberg
Subject a select statement that sometimes joins
Date
Msg-id Pine.BSF.4.33.0104101411440.82217-100000@nollie.summersault.com
Whole thread Raw
Responses Re: a select statement that sometimes joins
List 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 by date:

Previous
From: Joel Burton
Date:
Subject: Re: Re: \i command
Next
From: Najm Hashmi
Date:
Subject: Re: Re: \i command