a select statement that sometimes joins

From: Mark Stosberg <mark(at)summersault(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: a select statement that sometimes joins
Date: 2001-04-10 19:13:34
Message-ID: Pine.BSF.4.33.0104101411440.82217-100000@nollie.summersault.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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:

SELECT
messages.msg_id,
message_attachments.attachment_id
FROM messages,message_attachments
WHERE 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/

Browse pgsql-sql by date

  From Date Subject
Next Message Joel Burton 2001-04-10 19:14:25 Re: Re: \i command
Previous Message Joel Burton 2001-04-10 18:37:30 Re: \i command