Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Return addtional details in sql

Posted on 2014-11-27
2
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
melmers earned 1000 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 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 1000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

660 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