首先,微小的查询工作,但我需要在它的中间放置一些连接来创建一个条件。 微小的查询如下:
SELECT COUNT(vis.id), country.cod
FROM visits AS vis, countries AS country
WHERE country.description = vis.country
GROUP BY country.cod
现在的问题是我需要在where
子句中添加另一个条件,并且这个条件必然来自两个表中的连接。
SELECT COUNT(vis.id), country.cod
FROM visits AS vis, countries AS country
INNER JOIN products AS prod ON prod.id = vis.id_product
INNER JOIN customers AS cust ON cust.id = prod.id_customer
WHERE country.description = visit.country AND prod.id_customer = 13
GROUP BY country.cod
我收到的错误是:
#1054 - Unknown column 'vis.id_product' in 'on clause'
相信我,那个领域存在。此外,我已经尝试过,只是为了测试,将其他字段替换为id_product
而错误是相同的。
答案 0 :(得分:2)
您已为表visits
提供了别名vis
。现在你需要使用它。
SELECT COUNT(vis.id), country.cod
FROM visits AS vis JOIN
countries AS country
ON country.description = vis.country JOIN
products AS prod
ON prod.id = vis.id_product JOIN
customers AS cust
ON cust.id = prod.id_customer
WHERE prod.id_customer = 13
GROUP BY country.cod;
此外,您正在混合隐式和显式连接。仅使用显式连接,其中连接条件位于on
子句中,而不是where
子句中。一个简单的规则:永远不要在from
子句中使用逗号。