Two Foreign Keys of a Table Referring a Same Primary Key

i have a TB_Users table having userid(primary key) and UName (i.e.,username). I have another table TB_Devices having ProjectManager, & ReceivedBy fields referring to UserID of Users table. Now i have created a view via SQL Server Management Studio, & here is the code automatically generated:

SELECT     dbo.TB_Devices.ProjectName, dbo.TB_Devices.ProjectManager, dbo.TB_Devices.ReceivedBy, dbo.TB_Users.UName
FROM         dbo.TB_Users INNER JOIN
                      dbo.TB_Devices ON dbo.TB_Users.UserID = dbo.TB_Devices.ProjectManager AND dbo.TB_Users.UserID = dbo.TB_Devices.ReceivedBy

the problem is this code will give me only those rows where both ProjectManager and ReceivedBy are the same.

I found some results on google telling me to use aliases to distinguish between ProjectManager & the ReceivedBy fields in the Users table, but I couldn't do it
Can anyone tell me how to get through this?
tryMe2cAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OriNetworksCommented:
I would have my query as follows
SELECT     dbo.TB_Devices.ProjectName, pm.UName AS ProjectManager, RecBy.UName AS ReceivedBy
FROM         
                      dbo.TB_Devices 
LEFT JOIN dbo.TB_Users pm ON pm.TB_Users.UserID = TB_Devices.ProjectManager 
LEFT JOIN dbo.TB_Users  RecBy ON RecBy.TB_Users.UserID = TB_Devices.ProjectManager

Open in new window

0
tim_csCommented:
Try this.
SELECT
	a.ProjectName
	,a.ProjectManager
	,a.RecievedBy
	,b.UName ProjectManagerName
	,c.UName RecievedByName
FROM
	dbo.TB_Devices a
	INNER JOIN dbo.TB_Users b
		a.ProjectManager = b.UserID
	INNER JOIN dbo.TB_Users c
		ON a.RecievedBy = c.UserID

Open in new window

0
tryMe2cAuthor Commented:
sorry but it didn't work with me in either way

@tim_cs
I got this error message after running your query like in the image:

Error Message Image

http://imageshack.us/photo/my-images/705/errormsgm.jpg/

0
tim_csCommented:
I missed an ON in mine.
SELECT 
        a.ProjectName 
        ,a.ProjectManager 
        ,a.RecievedBy 
        ,b.UName ProjectManagerName 
        ,c.UName RecievedByName 
FROM 
        dbo.TB_Devices a 
        INNER JOIN dbo.TB_Users b 
                ON a.ProjectManager = b.UserID 
        INNER JOIN dbo.TB_Users c 
                ON a.RecievedBy = c.UserID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tryMe2cAuthor Commented:
Beautiful :D
Just aside note, you misspelled the ReceivedBy as RecievedBy, but I corrected it, & then it worked like magic

Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.