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

x
?
Solved

Trying to SELECT two unique IDs from one table

Posted on 2004-10-01
3
Medium Priority
?
195 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
[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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

705 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