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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Return addtional details in sql

Hi,

I have two tables, Users and Managers. The Users table contains all details of system users, including username. Primary key is UserID. The Managers table consists of two columns only, ManID and UserID.
The ManID in the Managers table is the same as the UserID in the Users Table

How would I link back to the Users table to return the username for the Manager for each user returned in the query?

Any help would be appreciated

Thanks
0
Morpheus7
Asked:
Morpheus7
2 Solutions
 
melmersCommented:
Assuming that your table are build like this
Users:
UID int,
UserName varchar
ForeName varchar
LastName varchar,

Managers:
ManID int
UID int

The query should show like this:
Select u1.UID, u1.UserName, u1.ForeName, u1.LastName, ManId, U2.UserName FROM Users as U1
LEFT OUTER JOIN Managers ON Manager.UID = U1.UID
LEFT OUTER JOIN Users AS U2 ON U2.UID = Managers.ManID

This shows you all Users. If one USer has no Manager then the field is NULL
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
SELECT U.UserID, U.UserName, M.UserID ManagerID, M.UserName ManagerName
FROM Users U
  INNER JOIN Managers UM 
     INNER JOIN Users M ON UM.ManID=M.UserID
  ON U.UserID=UM.UserID

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now