Solved

Join on the same table different column

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

932 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

14 Experts available now in Live!

Get 1:1 Help Now