?
Solved

DB2 Join shortcuts - isn't there a special way you can join if the column name is the SAME in the 2 tables being joined

Posted on 2014-09-24
8
Medium Priority
?
569 Views
Last Modified: 2014-09-25
I seem to recall reading there is a special way you can join if the column name is the SAME in the 2 tables being joined.  The perk to using this method is that when joining on this same column, the result set only gets the column one time.  So if you used this other syntax instead of "from t1 left join t2 on t2.mycol = t1.mycol" then "mycol" would only exist once in the joined result set.

Or maybe I just dreamed that?
0
Comment
Question by:Ray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 40342820
That's called a Natural Join, but I can't recall any popular DBMS supporting that. And it is always an Inner Join by definition.
0
 
LVL 11

Author Comment

by:Ray
ID: 40342830
whats the specific syntax because it's not the typical ...
from t1 left join t2 on t2.mycol = t1.mycol
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40342845
I don't get what you ask for.

Left Join is one kind of Outer Join, allowing one side (the right in this case) to be not matched and still return rows.

The Natural Join has theoretical syntax descriptions, but no implementation in DB2 and many more DBMS. It is "implemented" manually by enumerating the disjunct columns of at least one of the tables, so no change for you. And as said, Natural Join cannot be an Outer Join (left or right or full).
0
Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

 
LVL 18

Accepted Solution

by:
Dave Ford earned 2000 total points
ID: 40342886
It works for me on DB2 for i :

select *              
  from deleteme 
  join deleteme2
 using (commonColumn)

Open in new window


HTH,
DaveSlash
0
 
LVL 11

Author Closing Comment

by:Ray
ID: 40343891
"Using" is what I couldn't remember!  Thanks Dave :-)
0
 
LVL 11

Author Comment

by:Ray
ID: 40343893
Thanks for the effort Qlemo.
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 40344720
I realize this question is already answered, but here's another neat trick for shortening your queries that works beautifully on DB2 for i (v6.1) :

Instead of this:

SELECT someStuff
  FROM Table1 a
  left outer join Table2 b
    on b.column1 = a.column1
   and b.column2 = a.column2
  left outer join Table3 c 
    on c.column1 = b.column1
   and c.column2 = b.column2
   and c.column3 = b.column3
   and c.column4 = b.column4

Open in new window

You can do this:

SELECT someStuff
  FROM Table1 a
  left outer join Table2 b 
    on (b.column1,b.column2) = (a.column1,a.column2)
  left outer join Table3 c
    on  (c.column1,c.column2,c.column3,c.column4) =
        (b.column1,b.column2,b.column3,b.column4)

Open in new window


HTH,
DaveSlash
0
 
LVL 11

Author Comment

by:Ray
ID: 40344965
VERY nice Dave!!!  I hope I remember this long enough to use it and turn it into a habit.  Thanks for the extra 'tip'!
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question