views:

1347

answers:

4

When LEFT JOINing tables in a SQL query, sometimes I need to reference multiple tables in the ON clause. For example:

SELECT p.Name, j.Job, s.Salary
FROM PeopleTable p, JobTable j
LEFT JOIN SalaryTable s ON s.PeopleID=p.PeopleID AND s.JobID=j.JobID

However, the above would give this error:

SQL Error: The multi-part identifier "p.PeopleID" could not be bound.

It seems that the ON clause in a LEFT JOIN statement can only "see" the last table listed in the FROM list. Is this true? Any workarounds?

A: 

Try

SELECT p.Name, j.Job, s.Salary
FROM SalaryTable s
LEFT JOIN PeopleTable p on s.PeopleID = p.PeopleID
LEFT JOIN JobTable j ON s.JobID = j.JobID
Leahn Novash
A: 

Hi, i am not shure why si that but FROM PeopleTable p, JobTable j can be replaced with CROSS JOIN

SELECT p.Name, j.Job, s.Salary
FROM PeopleTable p
  CROSS JOIN JobTable j
  LEFT JOIN SalaryTable s ON s.PeopleID=p.PeopleID AND s.JobID=j.JobID

Best Regrads, Iordan

IordanTanev
that would be becasue he had it written as a cross join already inthe old obsolete syntax. I'm not convinced what he wrote is what he really needs though.
HLGEM
+2  A: 

You cannot mix the SQL-89 Join syntax "table, table" with the SQL-92 Join syntax "table LEFT JOIN table ON condition"

RBarryYoung
it seems you can mix them, but the order is important try : select *from msdb..sysjobsteps s , msdb..sysjobs jjoin msdb..syscategories c on j.category_id = c.category_idwhere j.job_id = s.job_id
Kev Riley
but mixing them in the way JerSchneid wants is obviously different!
Kev Riley
I'm guessing that the outer joins present irresolvable problems to compiler when trying to resolve the logic. (the specificity of outer join conditions being one of the things that is significantly different between the two).
RBarryYoung
In any event, it's really not a good idea to mix them anyway.
RBarryYoung
Definitely second that! Choose one (preferably SQL-92) and stick with it!
Kev Riley
+2  A: 

While the cross join syntax is a direct translation of what you provided, it may not be right for your situation. It would associated all people to all jobs before left joining to the salary table. This does not seem likely that this is what you want.

Do you really have any people who aren't associated with a salary? For that matter do you want to see any jobs which are not associated with a salary or people? Sample data and result set would help us give you a query that does what you really need. I suspect one of the following might give you better results:

SELECT p.Name, j.Job, s.Salary
FROM  PeopleTable p
JOIN SalaryTable s  ON s.PeopleID=p.PeopleID
RIGHT JOIN JobTable j  ON s.JobID=j.JobID

SELECT p.Name, j.Job, s.Salary
FROM  PeopleTable p
JOIN SalaryTable s  ON s.PeopleID=p.PeopleID
JOIN JobTable j  ON s.JobID=j.JobID

SELECT p.Name, j.Job, s.Salary
FROM SalaryTable s 
RIGHT JOIN PeopleTable p  ON s.PeopleID=p.PeopleID
RIGHT JOIN JobTable j  ON s.JobID=j.JobID
HLGEM
I actually just made those tables up as an example. I run into this type of table, table, left join issue every once in a while and I was just wondering if there where any tricks out there that I didn't know, other than rearranging my query.
JerSchneid