pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
JOIN syntax
5+ messages / 5 participants
[nested] [flat]

* JOIN syntax
@ 2000-06-25 23:05  Oliver Elphick <olly@lfix.co.uk>
  0 siblings, 1 reply; 5+ messages in thread

From: Oliver Elphick @ 2000-06-25 23:05 UTC (permalink / raw)
  To: pgsql-sql

Looking at SQL92, it seems that I ought to be able to do this:

SELECT * FROM invoice INNER JOIN (SELECT * FROM invoice_line WHERE lineno > 1) 
ON invoice.invno = invoice_line.invno;
ERROR:  parser: parse error at or near "("

(I know it can be successfully expressed in a more complex WHERE clause).

Is there anything wrong with the syntax, or is it a missing feature?

-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "Honour thy father and mother; which is the first  
      commandment with promise; That it may be well with 
      thee, and thou mayest live long on the earth."        
                          Ephesians 6:2,3 





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: JOIN syntax
@ 2000-06-26 04:11  Thomas Lockhart <lockhart@alumni.caltech.edu>
  parent: Oliver Elphick <olly@lfix.co.uk>
  0 siblings, 0 replies; 5+ messages in thread

From: Thomas Lockhart @ 2000-06-26 04:11 UTC (permalink / raw)
  To: Oliver Elphick <olly@lfix.co.uk>; +Cc: pgsql-sql

> Looking at SQL92, it seems that I ought to be able to do this:
> SELECT * FROM invoice INNER JOIN
>  (SELECT * FROM invoice_line WHERE lineno > 1)
>  ON invoice.invno = invoice_line.invno;
> ERROR:  parser: parse error at or near "("

Wrong syntax (for Postgres anyway). We don't yet have subselects in the
syntax. Thanks for the example though; it seems like some variant should
be allowed, though perhaps not as written above.

Something like

  select * from invoice inner join
    (select * from invoice_line where lineno > 1)
      as IL (invno, yada)
    on invoice.invno = IL.invno;

seems like it could be legal. Your variant of this may have trouble
hooking up invoice_line.invno after the subselect, since the subselect
result may lose the linkage with the underlying input table.

But, all this is theoretical, since Postgres doesn't yet do the right
thing at all.

                        - Thomas



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* join syntax
@ 2003-06-17 18:44  Jodi Kanter <jkanter@virginia.edu>
  0 siblings, 2 replies; 5+ messages in thread

From: Jodi Kanter @ 2003-06-17 18:44 UTC (permalink / raw)
  To: pgsql-sql

One of my colleagues has created a database where he has the same field 
name in two tables and uses this field to link his tables rather than 
some arbitrary value. For example, he has used "exp_id" in two tables. 
When writing his joins he uses a syntax that says something like JOIN ON 
EXP_ID. Can someone tell me what that syntax should be? I am not very 
familiar with it since I typically use the syntax where one field is set 
equal to the other.
Personally I prefer not to set databases up this way but cannot seem to 
convince him of this. And yet I am supposed to now help him with his 
database and application.. Is there some documentation that would define 
this type of syntax? How is this handled if you have more than one table 
in the join? It does not appear that this format would allow for this.
Thanks
Jodi
-- 

_______________________________
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
jkanter@virginia.edu <mailto:jkanter@virginia.edu>

 

 

 

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: join syntax
@ 2003-06-17 18:48  Josh Berkus <josh@agliodbs.com>
  parent: Jodi Kanter <jkanter@virginia.edu>
  1 sibling, 0 replies; 5+ messages in thread

From: Josh Berkus @ 2003-06-17 18:48 UTC (permalink / raw)
  To: Jodi Kanter <jkanter@virginia.edu>; pgsql-sql

Jodi,

> One of my colleagues has created a database where he has the same field 
> name in two tables and uses this field to link his tables rather than 
> some arbitrary value. For example, he has used "exp_id" in two tables. 
> When writing his joins he uses a syntax that says something like JOIN ON 
> EXP_ID. Can someone tell me what that syntax should be? I am not very 
> familiar with it since I typically use the syntax where one field is set 
> equal to the other.

table_a JOIN table_b USING (exp_id)

> Personally I prefer not to set databases up this way but cannot seem to 
> convince him of this. And yet I am supposed to now help him with his 
> database and application.. 

That, we can't help you with.

Point out to your friend, though, that an explicit JOIN order can often harm 
performance when the JOIN order given by the query writer is not ideal.  That 
is, the "FROM table, table, table WHERE expression, expression" syntax gives 
the parser a freer hand to choose the fastest execution method.  Of course, 
on a very small database that typically makes litte difference.

-- 
-Josh Berkus
 Aglio Database Solutions
 San Francisco




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: join syntax
@ 2003-06-17 18:53  scott.marlowe <scott.marlowe@ihs.com>
  parent: Jodi Kanter <jkanter@virginia.edu>
  1 sibling, 0 replies; 5+ messages in thread

From: scott.marlowe @ 2003-06-17 18:53 UTC (permalink / raw)
  To: Jodi Kanter <jkanter@virginia.edu>; +Cc: pgsql-sql

On Tue, 17 Jun 2003, Jodi Kanter wrote:

> One of my colleagues has created a database where he has the same field 
> name in two tables and uses this field to link his tables rather than 
> some arbitrary value. For example, he has used "exp_id" in two tables. 
> When writing his joins he uses a syntax that says something like JOIN ON 
> EXP_ID. Can someone tell me what that syntax should be? I am not very 
> familiar with it since I typically use the syntax where one field is set 
> equal to the other.
> Personally I prefer not to set databases up this way but cannot seem to 
> convince him of this. And yet I am supposed to now help him with his 
> database and application.. Is there some documentation that would define 
> this type of syntax? How is this handled if you have more than one table 
> in the join? It does not appear that this format would allow for this.

If you're joining two tables on a field that has the same name you can use 
a natural join.

select * from table1 natural join table2;




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2003-06-17 18:53 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-06-25 23:05 JOIN syntax Oliver Elphick <olly@lfix.co.uk>
2000-06-26 04:11 ` Thomas Lockhart <lockhart@alumni.caltech.edu>
2003-06-17 18:44 join syntax Jodi Kanter <jkanter@virginia.edu>
2003-06-17 18:48 ` Re: join syntax Josh Berkus <josh@agliodbs.com>
2003-06-17 18:53 ` Re: join syntax scott.marlowe <scott.marlowe@ihs.com>

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