Solved

Join on the same table different column

Posted on 2012-03-25
4
604 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
  • 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now