From: | "Aaron Bono" <postgresql(at)aranya(dot)com> |
---|---|
To: | "Joost Kraaijeveld" <J(dot)Kraaijeveld(at)askesis(dot)nl> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Can I do this smarter? |
Date: | 2006-07-14 03:52:39 |
Message-ID: | bf05e51c0607132052l6b95d5b1m67d5620e80e7491e@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
On 7/13/06, Joost Kraaijeveld <J(dot)Kraaijeveld(at)askesis(dot)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
==================================================================
From | Date | Subject | |
---|---|---|---|
Next Message | Joost Kraaijeveld | 2006-07-14 04:16:16 | Re: Can I do this smarter? |
Previous Message | Joost Kraaijeveld | 2006-07-14 03:41:28 | Can I do this smarter? |