Thread: nested inner join help

nested inner join help

From
"Darryl M. Wolski"
Date:
I  have a query which works perfectly fine in mysql but won't
work in the same database in postgres...
 
Can anyone take a look at this and tell me what the problem is?
 
I get a parser error at (.
 
SELECT orderlines.quantity, products.productname, products.productcost, customers.custlname,customers.custfname
FROM (((orders INNER JOIN orderlines ON orders.orderid = orderlines.lnkorderid)  INNER JOIN customers ON (customers.custid = orders.lnkcustid)) INNER JOIN products ON (products.productid = orderlines.lnkproductid))
WHERE (((orders.orderid)=2));
It seems as soon as I try to nest an INNER JOIN I get these parser errors. I have tried this query a few different ways, and still cant get it to work.
 
Thank you so much!!
 
Darryl Wolski

Re: nested inner join help

From
"Josh Berkus"
Date:
Darryl,

In Postgres, neither the parentheses nor the word "INNER" are required.  Youwill significantly simplify your life if
youget rid of them, and clean upyour presentation of SQL text in order to debug your own joins.
 

One significant MSAccess <==> Postgres conflict is that Postgres assumes thatparens in the FROM clause represent
subselects(I think), while MS Access usesparens to organize JOINs.  Postgres behavior is, of course, more
SQL92correct.

SELECT orderlines.quantity, products.productname, products.productcost,customers.custlname,customers.custfname
FROM orders JOIN orderlines ON orders.orderid = orderlines.lnkorderid   JOIN customers ON customers.custid =
orders.lnkcustid  JOIN products ON products.productid = orderlines.lnkproductid
 
WHERE orders.orderid=2;

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


Re: nested inner join help

From
Tom Lane
Date:
"Darryl M. Wolski" <drwolski@sympatico.ca> writes:
> Can anyone take a look at this and tell me what the problem is?

> I get a parser error at (.

> SELECT orderlines.quantity, products.productname, products.productcost, cus=
> tomers.custlname,customers.custfname
> FROM (((orders INNER JOIN orderlines ON orders.orderid =3D orderlines.lnkor=
> derid)  INNER JOIN customers ON (customers.custid =3D orders.lnkcustid)) IN=
> NER JOIN products ON (products.productid =3D orderlines.lnkproductid))
> WHERE (((orders.orderid)=3D2));

Looks fine to me.  Are you using an old Postgres version?  We didn't
support JOIN keywords until 7.1.
        regards, tom lane


Re: nested inner join help

From
Tom Lane
Date:
"Josh Berkus" <josh@agliodbs.com> writes:
> One significant MSAccess <==> Postgres conflict is that Postgres assumes that
>  parens in the FROM clause represent subselects (I think),

Huh?  Parenthesizing JOINs is perfectly valid, and I think it's good
style too in complex cases.  See the nearby "SQL Joins" thread for
an example of a query that's quite unreadable without 'em.
        regards, tom lane


Re: nested inner join help

From
"Josh Berkus"
Date:
Tom,

> Huh?  Parenthesizing JOINs is perfectly valid, and I think it's good
> style too in complex cases.  See the nearby "SQL Joins" thread for
> an example of a query that's quite unreadable without 'em.

Aha.  I see.  Of course, the OUTER JOINs here sort of require parens.

So, Darryl, you do want to use parens some of time.  Just to make it harder foryou to decide.  However, I would assert
thatin your example query they arenot necessary and actually make the syntax more difficult.
 

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco