Re: Query having issues...

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chris Bowlby <excalibur(at)hub(dot)org>
Cc: Jim Buttafuoco <jim(at)spectrumtelecorp(dot)com>, pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Query having issues...
Date: 2002-09-14 19:21:17
Message-ID: 10746.1032031277@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Chris Bowlby <excalibur(at)hub(dot)org> writes:
> Ok, the order did have effect on the query, might I suggest that it
> shouldn't matter :>

If you think that, then you are wrong.

> SELECT co.first_name, co.last_name, co.email_address,
> a.password, c.company_number
> FROM contact co, domain d
> LEFT JOIN account_info a ON (co.contact_id = a.contact_id)
> LEFT JOIN company c ON (co.company_id = c.company_id)
> WHERE d.domain_id = '666'
> AND d.company_id = co.company_id;

The interpretation of this command per spec is

FROM
contact co,
((domain d LEFT JOIN account_info a ON (co.contact_id = a.contact_id))
LEFT JOIN company c ON (co.company_id = c.company_id))

which perhaps will make it a little clearer why co can't be referenced
where you are trying to reference it. A comma is not the same as a JOIN
operator; it has much lower precedence.

It would be legal to do this:

FROM contact co JOIN domain d ON (d.company_id = co.company_id)
LEFT JOIN account_info a ON (co.contact_id = a.contact_id)
LEFT JOIN company c ON (co.company_id = c.company_id)
WHERE d.domain_id = '666';

This gets implicitly parenthesized left-to-right as

FROM ((contact co JOIN domain d ON (d.company_id = co.company_id))
LEFT JOIN account_info a ON (co.contact_id = a.contact_id))
LEFT JOIN company c ON (co.company_id = c.company_id)

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephen Bacon 2002-09-14 21:22:43 Recommended technique for large imports?
Previous Message Chris Bowlby 2002-09-14 19:06:21 Re: Query having issues...

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2002-09-14 20:44:18 Re: Multicolumn foreign keys need useless unique indices?
Previous Message Chris Bowlby 2002-09-14 19:06:21 Re: Query having issues...