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! | ||||||
|
11 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!
BloodyDark
October 15th, 2008 at 3:58 am
4Hi !
Thank you for this trick.
I also had this problem in a LEFT JOIN request…
Bye !
BloodyDark
John Bachir
October 25th, 2008 at 7:50 pm
5Thanks a lot for posting this, that error message was driving me crazy.
kitt
November 11th, 2008 at 4:31 am
6Valuable information.
jaredc
January 2nd, 2009 at 12:31 pm
7Saved my bacon! Thanks a million. I was banging my head against the wall for FAR too long. Glad Google could find this page! Thanks.
Dang
February 4th, 2009 at 1:38 am
8Wow, thanks a ton. Easy fix but a pain in the ass if you have a ton of queries and you are migrating.
Seems pretty stupid to not be backwards compatible for this.
damian
March 5th, 2009 at 9:49 am
9Thanks, solved my problem
Jaime
June 25th, 2009 at 1:48 pm
10Thanks, helped me out after a hour of getting crazy!
Twitter Trackbacks for beware of table alias in LEFT JOIN [krisgale.com] on Topsy.com
September 1st, 2009 at 6:45 am
11[…] beware of table alias in LEFT JOIN krisgale.com/beware-of-table-alias-in-left-join – view page – cached meanderings of a computer scientist on the brink of sheer insanity. * about — From the page […]
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar
if you want something done right… DIY. is proudly powered by WordPress - BloggingPro theme by: DesignDisease