Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joining several columns from same table

Author  Topic 

andyfraser
Starting Member

2 Posts

Posted - 2006-10-15 : 11:39:57
Hi,

Sorry if this has been asked before, I looked through the FAQ but could not find an answer to the following.

I have a Project table which contains amongst other fields, a CreatedByID field and a LastModifiedByID field. Both these fields point to a User table. What I would like to do is get the two usernames from a query on the project table.

I know how to get one usename using the following Sql command but how do I get access to the second username ?

SELECT Project.Name,User.Username FROM Project,User WHERE Project.ID=@id AND User.ID = Project.CreatedByID;

I hope my ramblings make sense

Andy

Andy

robvolk
Most Valuable Yak

15732 Posts

Posted - 2006-10-15 : 11:44:34
SELECT Project.Name, A.Username As CreatedBy, B.Username As LastModifiedBy
FROM Project
INNER JOIN User A ON A.ID = Project.CreatedByID
INNER JOIN User B ON B.ID = Project.LastModifiedByID
WHERE Project.ID=@id
Go to Top of Page

andyfraser
Starting Member

2 Posts

Posted - 2006-10-15 : 11:57:46
robvolk, you are a star , that worked perfectly - many thanks.

Andy
Go to Top of Page
   

- Advertisement -