pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aaron Bono <postgresql@aranya.com>
To: Joost Kraaijeveld <J.Kraaijeveld@askesis.nl>
Cc: pgsql-sql@postgresql.org
Subject: Re: Can I do this smarter?
Date: Thu, 13 Jul 2006 22:52:39 -0500
Message-ID: <bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com> (raw)
In-Reply-To: <1152848488.6045.13.camel@localhost>
References: <1152848488.6045.13.camel@localhost>

On 7/13/06, Joost Kraaijeveld <J.Kraaijeveld@askesis.nl> wrote:
>
> I have three tables: customers, salesorders and invoices. Customers have
> salesorders and salesorders have invoices ( child tables have foreign
> key columns to their parent).
>
> I want to get a list of all invoices with their customers. This what I
> came up with:
>
> select
> invoices.objectid,
> invoices.invoicenumber,
> invoices.invoicedate,
> (select customer from salesorders where objectid =
> invoices.salesorderobjectid),
> (select customernumber from customers where objectid = (select customer
> from salesorders where objectid = invoices.salesorderobjectid)),
> (select lastname from customers where objectid = (select customer from
> salesorders where objectid = invoices.salesorderobjectid))
> from invoices
>
> Can I do this smarter as the three subselects select the same customer
> three times and I would think that 1 time is enough?



SELECT
   invoices.objectid,
   invoices.invoicenumber,
   invoices.invoicedate,
   salesorders.customer,
   customers.customernumber,
   customers.lastname
FROM invoices
INNER JOIN salesorders ON (
   salesorders.objectid = invoices.salesorderobjectid
)
INNER JOIN customers ON (
   customers.objectid = salesorder.customer
)

You should do INNER and OUTER  joins for connecting the tables by their
foreign keys.

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
==================================================================

view thread (5+ messages)  latest in thread

Message-ID: <bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com>
Permalink:  ../bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com/
Also on:    postgresql.org/message-id/bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: postgresql@aranya.com, J.Kraaijeveld@askesis.nl
  Subject: Re: Can I do this smarter?
  In-Reply-To: <bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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