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
 Simple sql query question

Author  Topic 

wab0111
Starting Member

2 Posts

Posted - 2012-02-28 : 06:49:28
I am somewhat new to SQL queries. Limited exposure over the years but am working on learning more. I created the query below. Instead of presenting GroupID in the results, I would like to return the corresponding Groupname from the Groups table. How do I do that with this query? Username and Fullname are from the table Users. GroupID is from GroupMembers. GroupID and Groupname are both in the table Groups. Thanks.

select Username, FullName, GroupID from Users
join GroupMembers
on Users.UserID=GroupMembers.UserID order by Username

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-28 : 06:53:50
You should be able to join to the Groups table:
SELECT
Username,
FullName,
g.GroupName
FROM
Users
JOIN GroupMembers
ON Users.UserID = GroupMembers.UserID
INNER JOIN [Groups] g
ON g.GroupId = GroupMembers.GroupID
ORDER BY
Username
Go to Top of Page
   

- Advertisement -