Solved

Trying to SELECT two unique IDs from one table

Posted on 2004-10-01
3
189 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 250 total points
Comment Utility
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
Comment Utility
Awesome!  Thanks very much!
0
 
LVL 8

Author Comment

by:saoirse1916
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Backup 24 67
c# code 19 56
SQL Server issue connecting to named instance 6 40
Sql query to Stored Procedure 6 12
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Viewers will learn how the fundamental information of how to create a table.
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.

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

10 Experts available now in Live!

Get 1:1 Help Now