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

x
?
Solved

Trying to SELECT two unique IDs from one table

Posted on 2004-10-01
3
Medium Priority
?
197 Views
Last Modified: 2008-03-06
Cheers all,

My title isn't very descriptive of my situation, so hopefully I can do a bit better here.

First of all, here's my SQL statement:

SELECT tblShoutOut.soID, tblShoutOut.soShouter, tblShoutOut.soShoutee, tblEmployees.empFName, tblEmployees.empLName
FROM tblShoutOut, tblEmployees
WHERE tblShoutOut.soActive = 1 AND tblShoutOut.soShouter = tblEmployees.empUsername AND tblShoutOut.soShoutee = tblEmployees.empUsername

I've got 2 tables: tblShoutOut and tblEmployees.  The unique key in tblEmployees is empUsername.  In my other table, tblShoutOut, I've got several fields that I'm trying to SELECT from: tblShoutOut.soID, tblShoutOut.soShouter, tblShoutOut.soShoutee.  Also in my SELECT statement I'm grabbing two field from tblEmployees: tblEmployees.empFName, tblEmployees.empLName.

The trick is, since soShouter = empUsername AND soShoutee = (a different)empUsername, I'm not getting any results.  Is there a way to do something like this:

SELECT tblShoutOut.soID, tblShoutOut.soShouter, tblShoutOut.soShoutee, tblEmployees.empFName (for soShouter), tblEmployees.empLName (for soShouter), tblEmployees.empFName (for soShoutee), tblEmployees.empLName (for soShoutee)

Thanks for all your help!
0
Comment
Question by:saoirse1916
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
SoftEng007 earned 1000 total points
ID: 12202523
SELECT tblShoutOut.soID, tblShoutOut.soShouter, tblShoutOut.soShoutee, A.empFName as ShouterFName, A.empLName as ShouterLName,B.empFName as ShouteeFName, B.empLName as ShouteeLName
FROM tblShoutOut inner join tblEmployees A
ON  tblShoutOut.soShouter = A.empUsername
inner join tblEmployees B
ON tblShoutOut.soShoutee = B.empUsername
WHERE tblShoutOut.soActive = 1
0
 
LVL 8

Author Comment

by:saoirse1916
ID: 12202826
Awesome!  Thanks very much!
0
 
LVL 8

Author Comment

by:saoirse1916
ID: 12202836
I figured it had to be one of the joins...I'd used LEFT JOIN on another part of the site...now I know what INNER JOIN does.  Thanks again!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

916 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