when mixing standard selects containing table aliases with LEFT JOIN’s you need be aware of a critical change as of mysql 5… if you reference a table alias in an ON clause the same way you might have in verison 4, mysql may report an ‘unknown column’ error.
so, a query of the form:
SELECT a.id FROM alpha a, beta b
LEFT JOIN gamma g ON (g.id = a.gamma_id)
WHERE b.id = a.beta_id
would report error #1054 - Unknown column ‘a.gamma_id’ in ‘on clause’
whereas:
SELECT a.id FROM (alpha a, beta b)
LEFT JOIN gamma g ON (g.id = a.gamma_id)
WHERE b.id = a.beta_id
… would not.
in case you failed to detect the subtle difference between the two, it’s the inclusion of the now-apparently-very-important parentheses around the list of tables before the LEFT JOIN.
this ‘bug’ has been reported to mysql ab (even though they claim is not a bug).
| bookmark it! | ||||||
|
3 Responses
jay
April 8th, 2008 at 12:35 pm
1What happens when you put parens in a normal query? I am not an SQL guy at all, but PostgreSQL seems to have some weird reactions:
django=# SELECT p.id FROM blog_post p, blog_tag t;
… (results as expected)
django=# SELECT p.id FROM (blog_post p, blog_tag t);
ERROR: syntax error at or near “,” at character 30
LINE 1: SELECT p.id FROM (blog_post p, blog_tag t);
Now I’m not sure that PostgreSQL allows for that parenthesized syntax at all, but it seems strange that somewhere in the grammar of MySQL it will allow or not allow ()’s to be left out depending on context.
krisgale
April 8th, 2008 at 1:08 pm
2yep i agree, it appears that with version 5 mysql ab has indeed abandoned ANSI standards for sql, even if only slightly, i don’t like the precedent that this sets.
Henrik
August 12th, 2008 at 11:06 am
3Thanks for this info! would never have figured it out by reading the mysql docs, great info!
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar