pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aaron Bingham <bingham@cenix-bioscience.com>
To: Aaron Bono <postgresql@aranya.com>
Cc: Joost Kraaijeveld <J.Kraaijeveld@askesis.nl>
Cc: pgsql-sql@postgresql.org
Subject: Re: Can I do this smarter?
Date: Fri, 14 Jul 2006 16:07:37 +0200
Message-ID: <44B7A529.6090201@cenix-bioscience.com> (raw)
In-Reply-To: <bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com>
References: <1152848488.6045.13.camel@localhost>
	<bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com>

Aaron Bono wrote:

> On 7/13/06, *Joost Kraaijeveld* <J.Kraaijeveld@askesis.nl 
> <mailto: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.

You can also rewirite this (IMO) more clearly thus:

SELECT
   invoices.objectid,
   invoices.invoicenumber,
   invoices.invoicedate,
   salesorders.customer,
   customers.customernumber,
   customers.lastname
FROM invoices, salesorders, customers
WHERE salesorders.objectid = invoices.salesorderobjectid
   AND customers.objectid = salesorder.customer;

-- 
--------------------------------------------------------------------
Aaron Bingham
Senior Software Engineer
Cenix BioScience GmbH
--------------------------------------------------------------------




view thread (5+ messages)  latest in thread

Message-ID: <44B7A529.6090201@cenix-bioscience.com>
Permalink:  ../44B7A529.6090201@cenix-bioscience.com/
Also on:    postgresql.org/message-id/44B7A529.6090201@cenix-bioscience.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: bingham@cenix-bioscience.com, postgresql@aranya.com, J.Kraaijeveld@askesis.nl
  Subject: Re: Can I do this smarter?
  In-Reply-To: <44B7A529.6090201@cenix-bioscience.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