Link to home
Start Free TrialLog in
Avatar of MontanaProspector
MontanaProspectorFlag for United States of America

asked on

How do I join multiple fields in one table to a single field on another table?

I know the solution to this is going to be embarrassingly easy, but my brain seems to be stuck.  I have two tables, one called "Projects" and one called "Employees".  There are several employees associated with each project so I have fields in the project table called "Owner", "Originator", "Customer", etc. where only the employee ID is stored in the "Projects" table.  In the "Employees" table I of course have the fields for each employee, "EmployeeID", "LastName", "FirstName".  When I do the query to pull up a list of projects, I want to be able to show the owner's first and last name, the originator's first and last name, the customer's first and last name, etc.

If it was just the owner, the query would look like this:

SELECT tblProjects.ProjectName, [tblEmployees]![FirstName]+" "+[tblEmployees]![LastName] AS Owner
FROM tblEmployees INNER JOIN tblProjects ON tblEmployees.ID = tblProjects.Owner;


I can't figure out how to display the originator and customer similarly.

Also, if anyone who answers this would tell me how I could have asked this question better I would appreciate it.

Avatar of MontanaProspector
MontanaProspector
Flag of United States of America image

ASKER

I realize I've mixed Microsoft Access syntax in there, but I'm sure anyone reading this gets the drift.
Avatar of kaufmed
I can't remember if this worked (I though I tried it once) and I don't have a way to test it right now, but when you want to join on many-to-many, you use an "AND" in your "ON" clause--what if you try using an "OR"?

SELECT tblProjects.ProjectName, [FirstName]+' '+[LastName] AS Owner
FROM tblEmployees
INNER JOIN tblProjects
ON tblEmployees.ID = tblProjects.Owner OR tblEmployees.ID = tblProjects.Originator OR tblEmployees.ID = tblProjects.Cutsomer;

Open in new window


Alternatively, instead of actually using the word "JOIN", you could just list your multiple tables and "join" them by way of the "WHERE" clause.

SELECT tblProjects.ProjectName, [FirstName]+' '+[LastName] AS Owner
FROM tblEmployees, tblProjects
WHERE tblEmployees.ID = tblProjects.Owner OR tblEmployees.ID = tblProjects.Originator OR tblEmployees.ID = tblProjects.Cutsomer;

Open in new window

Well so far I've been able to solve it using multiple views, each depending on the previous and using unique aliases for the names at every stage.  So there's a view called "vwOwner" that uses all of the fields from "tblProjects" joined to "tblEmployees" on Owner=EmployeeID with LastName and FirstName aliased as OwnerLast and OwnerFirst.  Then there's a view called "vwOriginator" that uses all of the fields from "vwOwner" joined to "tblEmployees" on Originator=EmployeeID with LastName and FirstName aliased as OriginatorLast and OriginatorFirst. And so on.  This seems a bit clunky to me (especially since the Employee table resides on a completely different server).  Is there a better way to do it?
Thanks for your reply on a Sunday, kaufmed.  Neither of your examples are going to give me the first and last names of all three people involved, Owner, Originator, Customer, for each project.
Sure they will, but they will show as different rows, not one single row. Is that what you are trying to achieve?
Hmmm.  You're correct, they are showing up in different rows (for some reason only 2 of the three are for me, but I'm sure it's just a typo) but they are all listed as "Owner".

I'm trying to make each project come up as a single row, with Owner, Originator, and Customer listed (and differentiated) in each row.
Oh, and I just figured out why only 2 of the 3 people are showing up...so far the Originator and the Customer have been the same for each project.  I want to have provisions for those people to be different.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh baby.  That is SOOOO much nicer than the way I was doing it.  Thanks angellll.

As an SQL noob, it looks to me like this wouldn't work without using aliases correct?  Or am I thinking of this right?  Is o an alias for the employee table or the JOIN?

Also, did I state my question in such a way that you could understand what I was asking without having to read the question?  How would you have asked it differently?

He hit it out of the park, as usual.
To answer your last questions: Yes, o and r are aliases, and as they are not only used in the join but also in the list of fields selected, they are not aliases for the join (there is no such thing) they are table aliases. The verbose syntax is tablename AS tablealias (here for example "tblProjects AS p" would also be valid). This is just like with aliasnames of fields (here: AS Owner and AS Originator).

And in regard to the question: It was understandable, you just didn't specify how you would have liked the different persons to appear: In the same or additional records of the result.

What you do here simply is a multiple join of the same table. It's still only one physical table, but with two aliases o and r, which each applying to the join condition. You will have it hard to use the same table more than once without an alias :). Yes, it's a must, otherwise you couldn't give different join conditions and adress the employee being the owner vs. being the originator etc.

Bye, Olaf.
Thanks for the additional follow-up Olaf.
glad I could help.

the question was easily understandable to people having the experience of having done this kind of thing already.

for others, and usually for most questions, sample data with the relevant output requested can help to clarify much easier than tons of words and trying to explain in plain english :)

CHeers
If I could give an example of the desired output, I wouldn't have needed to ask the question <grin>.

Thanks you guys for helping me make EE a better place to find answers.