Solved

Return addtional details in sql

Posted on 2014-11-27
2
184 Views
Last Modified: 2014-11-27
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
Comment
Question by:Morpheus7
2 Comments
 
LVL 11

Accepted Solution

by:
melmers earned 250 total points
ID: 40468691
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
 
LVL 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 40468800
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now