SELECT w.title, c.CustomerID, COUNT(o.form_id) as cnt
FROM warehouse w
JOIN customer c USING (countrycode)
JOIN `Order` o USING (CustomerID)
GROUP BY w.siteid, c.CustomerID
ORDER BY w.title ASC, c.CustomerID
编辑
如果要包括客户没有任何订单,则需要一个LEFT JOIN Customers表:
SELECT w.title, c.CustomerID, COUNT(o.form_id) as cnt
FROM warehouse w
JOIN customer c USING (countrycode)
LEFT JOIN `Order` o USING (CustomerID)
GROUP BY w.countrycode, c.CustomerID
ORDER BY w.title ASC, c.CustomerID
若要筛选,只有一个客户:
SELECT w.title, c.CustomerID, COUNT(o.form_id) as cnt
FROM warehouse w
JOIN customer c USING (countrycode)
LEFT JOIN `Order` o USING (CustomerID)
WHERE c.CustomerId = 2
GROUP BY w.countrycode, c.CustomerID
ORDER BY w.title ASC, c.CustomerID
您可能想要提供一些示例数据和所需的结果,而不是链接到没有人可以测试或验证结果是否正确的图像。 http://sqlfiddle.com/非常适合设置测试,但样本数据和期望的结果应该成为未来访问者有类似问题的参考。 –