Solved

Join on the same table different column

Posted on 2012-03-25
4
613 Views
Last Modified: 2012-03-25
Is it possible to JOIN on the same table, but different column in the same query?

SELECT Table1.Name, Table2.FirstName
FROM Table1
INNER JOIN Table2
ON Table1.Table1ID = Table2.FirstID
INNER JOIN Table2
ON Table1.Table1ID = Table2.SecondID

I know the above scenario doesn't work, so how could I JOIN on the same table different column?
0
Comment
Question by:mitdaniels
[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
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 37764133
use a LEFT JOIN

SELECT Table1.Name, Table2.FirstName
FROM Table1
INNER JOIN Table2
ON Table1.Table1ID = Table2.FirstID
LEFT JOIN Table2
ON Table1.Table1ID = Table2.SecondID
0
 

Author Comment

by:mitdaniels
ID: 37764219
Thanks aneeshattingal.

I gave it a go, but get this error message:

"Msg 1013, Level 16, State 1, Line 1
The objects "Table2" and "Table2" in the FROM clause have the same exposed names. Use correlation names to distinguish them."
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 37764272
SELECT Table1.Name, Table2.FirstName
FROM Table1
INNER JOIN Table2  
ON Table1.Table1ID = Table2.FirstID
LEFT JOIN Table2 t
ON Table1.Table1ID = t.SecondID
0
 

Author Closing Comment

by:mitdaniels
ID: 37764287
The concept works thanks!!
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

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…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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…

726 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