Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

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.

0
MontanaProspector
Asked:
MontanaProspector
  • 9
  • 2
  • 2
  • +1
1 Solution
 
MontanaProspectorAuthor Commented:
I realize I've mixed Microsoft Access syntax in there, but I'm sure anyone reading this gets the drift.
0
 
käµfm³d 👽Commented:
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

0
 
MontanaProspectorAuthor Commented:
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?
0
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 
MontanaProspectorAuthor Commented:
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.
0
 
käµfm³d 👽Commented:
Sure they will, but they will show as different rows, not one single row. Is that what you are trying to achieve?
0
 
MontanaProspectorAuthor Commented:
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.
0
 
MontanaProspectorAuthor Commented:
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.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you will have to join  the person table several times ...

SELECT p.ProjectName
  , o.[FirstName] + o.[LastName] AS Owner
  , r.[FirstName] + r.[LastName] AS Originator
FROM tblProjects p
INNER JOIN tblEmployees o
  ON o.ID = e.Owner
INNER JOIN tblEmployees r
  ON r.ID = e.Originator

etc

Open in new window

0
 
MontanaProspectorAuthor Commented:
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?

0
 
MontanaProspectorAuthor Commented:
He hit it out of the park, as usual.
0
 
Olaf DoschkeSoftware DeveloperCommented:
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.
0
 
MontanaProspectorAuthor Commented:
Thanks for the additional follow-up Olaf.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
0
 
MontanaProspectorAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 9
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now