Forum Moderators: open

Message Too Old, No Replies

LEFT OUTER JOIN foobar

after upgrade to MySQL V5

         

apetit

1:41 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



We had a server hard drive crash recently, and the OS reload led to mySQL being upgraded from V4 something to 5.0.26 without plan :(
("Downgrading MySQL from 5 to 4 will be a non-trivial exercise", says the admin, "find another solution" :( )
I'm finding that queries that were working perfectly well before, now
aren't. We were able to save all data, it was only the versions of installed software that changed.
The query appears to be having a problem with the ON clause of a LEFT OUTER JOIN
eg.
SELECT *
FROM EmploymentRecords AS er, Employees AS e, user AS u
LEFT OUTER JOIN restaurants AS r ON r.idRestaurant = er.idRestaurant
WHERE u.idPerson = e.idPerson
AND er.idEmployee = e.idEmployee
AND u.idUser = 'john.smith'
ORDER BY er.dtStarted DESC
LIMIT 0 , 100
MySQL said:
#1054 - Unknown column 'er.idRestaurant' in 'on clause'

*but!*

SELECT *
FROM EmploymentRecords AS er, Employees AS e, user AS u
WHERE u.idPerson = e.idPerson
AND er.idEmployee = e.idEmployee
AND u.idUser = 'lea.de.groot'
ORDER BY er.dtStarted DESC
works fine

and
SELECT *
FROM EmploymentRecords AS er
LEFT OUTER JOIN restaurants AS r ON r.idRestaurant = er.idRestaurant
works fine

(ER:
An EMPLOYEE will:
- have one USER record, continaing username and password, (1 - 1)and
- have an EMPLOYMENTRECORD for each time they are employed or change
restaurants (1 - many))

I am quite bemused by what could be causing this.
Anyone have any idea?
(Help!)

coopster

5:11 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, apetit.


INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).

However, the precedence of the comma operator is less than than of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.

[dev.mysql.com...]

Page down to see the compliance with SQL:2003 standard and how to rewrite your query accordingly. Scan for the word "precedence" on the page and you'll find your answer.

apetit

2:12 am on Jan 18, 2007 (gmt 0)

10+ Year Member



Hurrah!
Thank you coopster, that was it, and thank you for the welcome :)
I love an easy fix :)