INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

extracting values from same field different row using different where statements

extracting values from same field different row using different where statements

extracting values from same field different row using different where statements

(OP)
I have a table called `employment` that contains two INT references named `employer` and `employee`. As a person can be either or both I have a single table named `personnel`.

I am trying to construct a query that displays

personnel.name as `employer` where employment.employer=personnel.id

AND

personnel.name as `employee` where employment.employee=personnel.id

so it extracts multiple details from one table with different where conditions and applying different alias values

Very very new to sql so my head is starting to spin

For example my personnel file:

id name
1 David
2 Simon
3 Michael
4 Tony
5 Paul

employment file:

Job 1, Title "housea" employer 2 employee 5 - I would be looking to return the values
[1][housea][Simon][Paul]

Job 2, Title "houseb" employer 1 employee 2 - I would be looking to return the values
[1][houseb][David][Simon]

If you understand what I am trying to do can you please help

RE: extracting values from same field different row using different where statements

A backtick as name delimiter always points me to MySQL, are you posting in the right forum section?

Aside of MSSQL or MySQL, the general approach to joining a table with two foreign keys (employee, employer) both pointing to one table (personnel) is doing two joins to it, joining with a table alias name, eg

CODE -->

Select employment.job, employment.title, employment.employee, employees.name as employeename, employment.employer, employers.name as employername
from employment 
left join personnel as employees on employees.id = employment.employee
left join personnel as employers on employers.id = employment.employer 

Bye, Olaf.

RE: extracting values from same field different row using different where statements

Doesn't it work for you? Too many result columns, wrongly named? That's a no brainer, simply remove unwanted result columns from code line 1 and/or rename them, too. The main ingredient is the double joining of personnel.

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close