简体   繁体   中英

What's wrong with my SQL?

Sorry for such a generic question, but I'm really struggling to find out why I'm getting the error Unknown column 'tbl_downloads.itemid' in 'on clause' . There is absolutely a column called itemid in the tbl_downloads table.

SELECT tbl_downloads.itemid,COUNT(tbl_downloads.itemid*temp1.score) AS score2
FROM tbl_downloads,temp1
LEFT JOIN temp2 ON tbl_downloads.itemid=temp2.itemid
WHERE temp2.itemid IS NULL
AND tbl_downloads.memberid=temp1.memberid
GROUP BY tbl_downloads.itemid
ORDER BY score2 DESC
LIMIT 50;

It's better to be consistent in your use of implicit and explicit join syntax - this would be an improvement:

SELECT tbl_downloads.itemid, COUNT(tbl_downloads.itemid*temp1.score) AS score2
FROM tbl_downloads
JOIN temp1 ON tbl_downloads.memberid=temp1.memberid
LEFT JOIN temp2 ON tbl_downloads.itemid=temp2.itemid
WHERE temp2.itemid IS NULL
GROUP BY tbl_downloads.itemid
ORDER BY score2 DESC
LIMIT 50;

I'm really struggling to find out why I'm getting the error Unknown column 'tbl_downloads.itemid' in 'on clause'

The LEFT JOIN applies to temp1 and temp2 . The problem will become clearer if you format the query as follows:

SELECT ...
FROM tbl_downloads,
(temp1 LEFT JOIN temp2 ON tbl_downloads.itemid=temp2.itemid)
...

Thus tbl_downloads is not in scope in the ON clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM