pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
a select statement that sometimes joins
2+ messages / 2 participants
[nested] [flat]

* a select statement that sometimes joins
@ 2001-04-10 19:13  Mark Stosberg <mark@summersault.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Mark Stosberg @ 2001-04-10 19:13 UTC (permalink / raw)
  To: 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/





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: a select statement that sometimes joins
@ 2001-04-10 20:05  Oliver Elphick <olly@lfix.co.uk>
  parent: Mark Stosberg <mark@summersault.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Oliver Elphick @ 2001-04-10 20:05 UTC (permalink / raw)
  To: Mark Stosberg <mark@summersault.com>; +Cc: pgsql-sql

Mark Stosberg wrote:
  >
  >
  >
  >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"

SELECT m.msg_id, a.attachment_id
  FROM messages AS m
       LEFT OUTER JOIN message-attachments AS a
         ON m.msg_id = a.msg_id;

This requires 7.1 for the LEFT OUTER JOIN.  In 7.0.3 you could do it
with a UNION.

-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "But seek ye first the kingdom of God, and his  
      righteousness; and all these things shall be added  
      unto you."     Matthew 6:33 





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-04-10 20:05 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-04-10 19:13 a select statement that sometimes joins Mark Stosberg <mark@summersault.com>
2001-04-10 20:05 ` Oliver Elphick <olly@lfix.co.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox