Comments on: How to write SQL JOIN clauses more compactly http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/ Stay curious! Mon, 13 May 2013 05:55:40 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Alexandru Toth http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-16574 Alexandru Toth Sat, 20 Jun 2009 19:38:22 +0000 http://www.xaprb.com/blog/?p=267#comment-16574 Hi,

You said: The statement is valid and will execute, but it won’t give the results you probably wanted (tbl4’s join clause doesn’t refer to any columns from tbl4).

select tbl1.col1, tbl2.col2, tbl3.col2, tbl4.col1
from apples as tbl1
inner join oranges as tbl2 on tbl1.col3 = tbl2.col3
inner join grapes as tbl3 on tbl3.col3 = tbl2.col3
inner join peaches as tbl4 on tbl3.col3 = tbl2.col3

Fully agree, and would like to show the diagram generated by the open source project revj : http://snowflakejoins.com/static/xaprb.png

In the diagram it is quite evident that there is a cartezian join

You can try the tool online at:http://snowflakejoins.com/revj/index

Cheers, Alex
http://sourceforge.net/projects/revj/

]]>
By: rye http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-5752 rye Sun, 15 Apr 2007 07:24:27 +0000 http://www.xaprb.com/blog/?p=267#comment-5752 Well another difference between join with ‘on’ and join with ‘using’ is that when “select *” is used, with “on” the joined column will be presented twice, while with “using’ only once and placed as the first column.

Another point I think noteworthy is that with “using” the joined column must be enclosed in parenthesis/”()”, without them it’s an error.

ok the DBMS I play with is mysql 5.0.27 on NT.

]]>
By: Binary Look » links for 2006-12-09 http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2757 Binary Look » links for 2006-12-09 Sat, 09 Dec 2006 22:22:05 +0000 http://www.xaprb.com/blog/?p=267#comment-2757 [...] How to write SQL JOIN clauses more compactly – Xaprb Написание компактного кода SQL применительно к JOIN. (tags: sql mysql join select код программирование базы_данных) [...]

]]>
By: Xaprb http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2667 Xaprb Wed, 29 Nov 2006 19:30:05 +0000 http://www.xaprb.com/blog/?p=267#comment-2667 Rudy, very good points.

Disclosure: there is not a single USING in any codebase I’m involved with.

]]>
By: rudy http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2659 rudy Wed, 29 Nov 2006 13:30:48 +0000 http://www.xaprb.com/blog/?p=267#comment-2659 i don’t like USING either, because it’s vague and can, in certain circumstances, produce ridiculous results

in the example you gave:

select tbl1.col1, tbl2.col2, tbl3.col2, tbl4.col1
from apples as tbl1
   inner join oranges as tbl2 using(col3)
   inner join grapes as tbl3 using(col3)
   inner join peaches as tbl4 using(col3)

you say that “USING matches the specified columns from each table” but this is problematic — is it really each table?

in this case, col3 exists in all 4 tables, so does this mean that the last USING is equivalent to:

 on tbl4.col3 = tbl3.col3
and tbl4.col3 = tbl2.col3
and tbl4.col3 = tbl1.col3

and don’t get me started on NATURAL joins — what were they thinking?!!!

]]>