我的SQL出了什么问题?

时间:2013-04-10 13:17:09

标签: mysql sql

很抱歉这样一般的问题,但我真的很难找出为什么我收到错误Unknown column 'tbl_downloads.itemid' in 'on clause'。在tbl_downloads表中绝对有一个名为itemid的列。

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;

2 个答案:

答案 0 :(得分:8)

最好在使用隐式和显式连接语法时保持一致 - 这将是一个改进:

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;

答案 1 :(得分:4)

  

我真的很难找出为什么我收到错误Unknown column 'tbl_downloads.itemid' in 'on clause'

LEFT JOIN适用于temp1temp2。如果您按如下方式格式化查询,问题将变得更加清晰:

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

因此tbl_downloads不在ON子句的范围内。