Wolfeatspotatoes
Wolfeatspotatoes

Reputation: 135

Refering two columns with data from different table

I am having trouble with my SQL homework. I am using Microsoft SQL server management studio 17. In my table, I have a column for CustomerID1 and CustomerID2. I want to connect the id with another table called Customer and it has the Id as primary key and fullname. The output that I want is Customer1Name and Customer2Name. How do I connect two tables?

select c.FullName from Sales s, Customer c where s.CustomerID1 = c.Id

select c.FullName from Sales s, Customer c where s.CustomerID2 = c.Id

This is what i am trying to do but i wanted it to be in one sentence if it is possible. Thanks in advance!

Upvotes: 0

Views: 52

Answers (1)

Miguel Carreira
Miguel Carreira

Reputation: 526

Try the following:

select c1.FullName as Customer1Name, c2.FullName as Customer2Name
from Sales s
left join Customer c1 
on s.CustomerID1 = c1.Id 
left join Customer c2 
on s.CustomerID2 = c2.Id 

Upvotes: 2

Related Questions