Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 10-31-2005, 01:06 AM   PM User | #1
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 465
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
MySQL 5 Left Joins not working

I recently upgraded from MySQL 4.1 to 5.0.15. However, to my horror I have discovered that no LEFT JOINs work anymore.

For instance, the error #1054 is produced with the message Unknown column 'items.item_id' in 'on clause' when the following left join is executed:
Code:
SELECT items.itemid, items.title, md.keywords, cat.category
FROM items_table AS items, meta_data_table AS md, categories_table AS cat
LEFT JOIN restricted_table AS rst ON items.item_id=rst.item_id
WHERE items.title LIKE 'the%'
     AND md.item_id=items.item_id
     AND cat.cat_id=items.cat_id
     AND rst.item_id IS NULL
GROUP BY items.item_id
ORDER BY items.title DESC
LIMIT 10
This query worked fine in MySQL 4.1 and the current 5.0 documentation doesn't seem to indicate that it shouldn't. What could have happened to cause this?

Last edited by bauhsoj; 10-31-2005 at 01:58 AM..
bauhsoj is offline   Reply With Quote
Old 10-31-2005, 04:03 AM   PM User | #2
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 465
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
Strangely I have found that removing all references to "meta_data_table" and "categories_table" cause the error Unknown column 'items.item_id' in 'on clause' to stop occurring. How, I haven't a clue, seeing as the error message mentions nothing of these other tables. Additionally, removing those tables from the query is not an option in any case due to the fact that they retrieve essential information.

Anyone have any clues?

Thanks!
bauhsoj is offline   Reply With Quote
Old 10-31-2005, 07:11 PM   PM User | #3
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 465
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
In case anyone needs to know, this is on Windows XP SP2, Apache 2.0.54, and PHP 5.0.4 with an AMD 64 processer.
bauhsoj is offline   Reply With Quote
Old 10-31-2005, 10:25 PM   PM User | #4
Kid Charming
Regular Coder

 
Join Date: Jun 2005
Posts: 804
Thanks: 0
Thanked 0 Times in 0 Posts
Kid Charming is an unknown quantity at this point
I don't have the experience with 5 to say for sure, but my first guess would be that it's assuming you want to make the LEFT JOIN from the table listed immediately before the JOIN call, which is categories_table. try rearranging your query to put items right before the LEFT JOIN.
Kid Charming is offline   Reply With Quote
Old 10-31-2005, 11:29 PM   PM User | #5
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 465
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
Quote:
Originally Posted by Kid Charming
I don't have the experience with 5 to say for sure, but my first guess would be that it's assuming you want to make the LEFT JOIN from the table listed immediately before the JOIN call, which is categories_table. try rearranging your query to put items right before the LEFT JOIN.
Actually that solved the problem.

I wonder why MySQL 5 would be pickier....
bauhsoj is offline   Reply With Quote
Old 08-20-2008, 05:13 PM   PM User | #6
taagangel
New to the CF scene

 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
taagangel is an unknown quantity at this point
MySQL 5 is pickier because the new engine tries to match the ANSI SQL standard so it changes the order of how some joins need to be set up.

Last edited by taagangel; 08-20-2008 at 05:14 PM.. Reason: syntax error ;-)
taagangel is offline   Reply With Quote
Old 08-20-2008, 07:59 PM   PM User | #7
guelphdad
Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,137
Thanks: 3
Thanked 81 Times in 75 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Taagangel

Welcome to Coding Forums. As an FYI the thread you resurected is three years old. Check the posting dates from the left hand side of the posts.
guelphdad is offline   Reply With Quote
Old 02-12-2009, 04:54 PM   PM User | #8
Nyquest
New to the CF scene

 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Nyquest is an unknown quantity at this point
I know this thread has been long done and used, but I figured if anyone else is having this problem, I'd share the correct fix.

Kid Charming is correct that the LEFT JOIN is trying to use the table listed in the query, but the fix is actually very simple.

Change:
Code:
FROM items_table AS items, meta_data_table AS md, categories_table AS cat
To:
Code:
FROM (items_table AS items, meta_data_table AS md, categories_table AS cat)
in all applicable area's. (yes, I just added parenthesis)

Regards,
Nyquest is offline   Reply With Quote
Old 02-12-2009, 05:42 PM   PM User | #9
guelphdad
Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,137
Thanks: 3
Thanked 81 Times in 75 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Actually if you are going to correct something use the best solution and that is to get out of the habit of using comma join syntax entirely. Use explicit joins and ON clauses at all times.

Code:
FROM 
  items_table AS items
INNER JOIN
  meta_data_table AS md
ON
  md.item_id=items.item_id
INNER JOIN
  categories_table AS cat
ON
  cat.cat_id=items.cat_id
LEFT JOIN 
  restricted_table AS rst 
ON 
  items.item_id=rst.item_id
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:05 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.