Solved

Return addtional details in sql

Posted on 2014-11-27
2
181 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 45

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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