Solved

Return addtional details in sql

Posted on 2014-11-27
2
183 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 46

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 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

22 Experts available now in Live!

Get 1:1 Help Now