I've inherited a website someone else built using MySQL 4 as the database and am trying to switch to a new server, runn开发者_C百科ing MySQL 5. I copied all files across and dumped the database from MySQL version 4 and then imported back into version 5.
Now, half of the website is working while the other half is not. I keep getting the following:
Unknown column 'a.id_art' in 'on clause'
Here's my query:
SELECT *, aks.nazwa as sekcja, ak.nazwa kategoria
FROM
artykuly a,
artykuly_kategorie ak,
artykuly_sekcje aks
LEFT JOIN artykuly_addons aad ON aad.id_art=a.id_art
WHERE a.id_art = '20' AND ak.id_sek = aks.id_sek AND a.id_kat = ak.id_kat
Why does the above work fine in MySQL version 4 but is a broken man in version 5?
Probably the combination of LEFT JOIN and joining with commas and filtering in the WHERE clause causes the problem. Try this:
SELECT *, aks.nazwa as sekcja, ak.nazwa kategoria
FROM
artykuly a INNER JOIN artykuly_kategorie ak ON a.id_kat = ak.id_kat
INNER JOIN artykuly_sekcje aks ON ak.id_sek = aks.id_sek
LEFT JOIN artykuly_addons aad ON aad.id_art=a.id_art
WHERE a.id_art = '20'
The precedence of the comma is now lower than explicit JOIN statements. Quoting the MySQL 5.0 JOIN Syntax page:
Previously, the comma operator (,) and JOIN both had the same precedence, so the join expression t1, t2 JOIN t3 was interpreted as ((t1, t2) JOIN t3). Now JOIN has higher precedence, so the expression is interpreted as (t1, (t2 JOIN t3))
So previously this was treated as
SELECT *,
aks.nazwa as sekcja,
ak.nazwa kategoria
FROM
(
(
artykuly a,
artykuly_kategorie ak0
),
artykuly_sekcje aks
)
LEFT JOIN artykuly_addons aad ON aad.id_art=a.id_art
WHERE a.id_art = '20'
AND ak.id_sek = aks.id_sek
AND a.id_kat = ak.id_kat
Now it is interpreted as:
SELECT *,
aks.nazwa as sekcja,
ak.nazwa kategoria
FROM
(
(
artykuly a,
artykuly_kategorie ak0
),
(artykuly_sekcje aks LEFT JOIN artykuly_addons aad ON aad.id_art=a.id_art)
)
WHERE a.id_art = '20'
AND ak.id_sek = aks.id_sek
AND a.id_kat = ak.id_kat
精彩评论