Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL syntax - multiple joins

Posted on 2013-11-18
2
Medium Priority
?
296 Views
Last Modified: 2013-11-19
Hi,

I have a query with an inner join pulling data from two tables. One of the tables has several fields containing User IDs. This is a job table, so there is createdbyuserID, assignedtoUserID, etc. I'd like to append the existing query to pull in the names of these folks from the Users table, but I'm not sure how I would join the same table twice or even 4 times to pull names from the same set of user records.

My current query like so:

SELECT Customers.CustomerID, Customers.CustomerFirstName, Customers.CustomerLastName, Customers.CustomerEmail, Customers.CustomerPhoneCell, Customers.CustomerPhoneHome, Customers.OKContactLater, Jobs.JobAddress1, Jobs.JobAddress2, Jobs.JobCity, Jobs.JobState, Jobs.JobZipCode, Jobs.AcceptedPrice, Jobs.PaintStartDate, Jobs.PaintEndDate FROM Customers INNER JOIN Jobs ON Customers.CustomerID = Jobs.CustomerID WHERE PaintEndDate between 1/1/2013 and 12/31/2013

Open in new window


If I wanted to key on Jobs.CreatedByID to pull in Users.FirstName and Users.LastName and return that, then use Jobs.AssignedToID to pull in Users.FirstName and Users.LastName for that second different ID, how would I wrap my joins?

Thank you

Bill
0
Comment
Question by:billium99
2 Comments
 
LVL 23

Accepted Solution

by:
Steve Wales earned 2000 total points
ID: 39657906
Join the query to the Users Table twice - aliasing it the first time as Users1 and the second time as Users2:

SELECT Customers.CustomerID, Customers.CustomerFirstName, Customers.CustomerLastName, Customers.CustomerEmail, Customers.CustomerPhoneCell, Customers.CustomerPhoneHome, Customers.OKContactLater, Jobs.JobAddress1, Jobs.JobAddress2, Jobs.JobCity, Jobs.JobState, Jobs.JobZipCode, Jobs.AcceptedPrice, Jobs.PaintStartDate, Jobs.PaintEndDate, Jobs.CreatedbyID, Users1.First_name, Users2.LastName, Jobs.AssignedToID, Users2.FirstName, Users2.LastName
FROM Customers 
INNER JOIN Jobs ON Customers.CustomerID = Jobs.CustomerID 
INNER JOIN Users Users1 on Jobs.CreatedByID = Users1.UserID
INNER JOIN Users Users2 on Jobs.AssignedToID = Users2.UserID
WHERE PaintEndDate between 1/1/2013 and 12/31/2013

Open in new window

0
 
LVL 1

Author Closing Comment

by:billium99
ID: 39659512
Perfect - thx!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

927 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