|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
MySQL 5.0 LEFT JOINs not working correctly
I recently upgraded from MySQL 4.1 to 5.0.15. However, to my suprise I have discovered that certain LEFT JOINs do not 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.item_id, 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. 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. I haven't a clue why, 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. My server specs are as follows: Windows XP SP2, Apache 2.0.54, and PHP 5.0.4 with an AMD 64 processer. Anyone have any ideas what could be wrong here? ![]() |
|
#2
|
||||
|
||||
|
Quote:
__________________
My blog Tutorials about OSS databases, DBMonster ... Contribute to OSS Development, fill bug reports! Developer Shed eSupport Commented my.ini/my.cnf (ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins Random data (with a bias) |
|
#3
|
||||
|
||||
|
i am guessing, but your problems might just go away if you rewrite the query completely using JOIN syntax
it's possible the optimizer may be getting confused because you are mixing a LEFT JOIN with old-school table list joins where the conditions are in the WHERE clause this might be a new bug, but who cares -- if it's not working, you want to find a workaround, not sit and wait for a bug to be resolved in any case you shouldn't've written the query with that mix of styles in 4.1 either in addition, you have another potentially serious problem in that your GROUP BY is invalid, it should really contain all the non-aggregate columns in the SELECT so try this -- Code:
SELECT items.itemid
, items.title
, md.keywords
, cat.category
FROM items_table AS items
INNER
JOIN meta_data_table AS md
ON items.item_id = md.item_id
INNER
JOIN categories_table AS cat
ON items.cat_id = cat.cat_id
LEFT OUTER
JOIN restricted_table AS rst
ON items.item_id = rst.item_id
WHERE items.title LIKE 'the%'
AND rst.item_id IS NULL
GROUP
BY items.itemid
, items.title
, md.keywords
, cat.category
ORDER
BY items.title DESC LIMIT 10
|
|
#4
|
|||
|
|||
|
Quote:
Having to do that could actually cause a problem. That is the simplest out of several key queries that are used throughout the site. Some of the more complex queries utilize about 36 different columns across multiple tables, query formatted assignments, and IF() conditional assignments performed by the database engine. Would I need to put all of those columns in the GROUP BY statement? Also, the items_table actually has columns for 8 different cat_id's (e.g. cat_id1, cat_id2, cat_id3, etc.) so how would I work that into the query if I needed cat.cat_id IN(items.cat_id1, items.cat_id2, .... etc.)? I also need to be certain that the resultant query will work on a server running MySQL 4.0. MySQL 5.0 is run on our test server and it could be some time before the live server is running version 5. |
|
#5
|
|||
|
|||
|
Quote:
Just a typo on my part when making this post. Sorry. |
|
#6
|
||||
|
||||
|
Quote:
Quote:
|
|
#7
|
|||
|
|||
|
I have the solution to this, but not (all of) the reasoning behind it.
MySQL 5 supports SQL2003 more strictly. The FROM section of your query should be enclosed in parenthesis () in order to have the behavior you expect from 4.1, etc. MySQL 5 interprets your initial query (from the first post) to mean that you are LEFT JOINing to the final table specified in your FROM clause, not all of them, which was the behavior in 4.x. By enclosing the FROM in parenthesis, you're indicating to the parser that you wish to LEFT JOIN against any table that's already been specified. This was poorly documented, and it took me a while to find it. MPEDrummer Last edited by mpedrummer : November 3rd, 2005 at 01:36 AM. |
|
#8
|
||||
|
||||
|
Quote:
|
|
#9
|
|||
|
|||
|
Here you go
Look at the comment by [8 Oct 12:12] Sergei Golubchik It's not really documentation, but it's the best I could find, and it turned out to be right ![]() MPEDrummer /edit - I coulda sworn they parsed links automatically... |
|
#10
|
||||
|
||||
|
thanks, that's a great exlanation
to quote from that page -- Quote:
in my opinion it would be better to rewrite the joins using JOIN syntax, like in post #3 |
|
#11
|
||||
|
||||
|
Quote:
I concur completely, there is no real reason to mix the two types of join syntax, the list type of join can get mixed up quite easily when you leave out a join condition in the where clause, or better yet where you have to do a "workaround" as above.
__________________
MySQL - reformat varchar dates to proper date type || MySQL - room vacancies || output results with column names with mysql only Latest row per group || Deleting duplicates || Gaps in auto increment column My site: sql help articles |
|
#12
|
|||
|
|||
|
Code:
FROM (t1 , t2) LEFT JOIN t3 ON (expr) will not work in most other DBMS since it is not valid SQL as defined in the standard. |