这就是我所拥有的;
SELECT c.customerFN, c.customerEmail, p.productName,
SUM(p.unitsonstock + p.unitsordered) AS "All Units"
FROM customer c
INNER JOIN order o
WHERE c.customerID=o.customerID
INNER JOIN orderDetails d
WHERE o.orderID=d.orderID
INNER JOIN product p
WHERE p.productCode=l.productCode
WHERE orderDate <= '2015-03-15'
ORDER BY productName;
当我输入此数据库时,数据库会在第四行引发“缺少关键字”错误。你能告诉我我失踪了吗
答案 0 :(得分:3)
而不是第5,7和9行中的WHERE
,您需要使用ON
。您还使用了函数SUM
,但没有GROUP BY
。像这样更改您的查询:
SELECT c.customerFN, c.customerEmail, p.productName,
SUM(p.unitsonstock + p.unitsordered) AS "All Units"
FROM customer c
INNER JOIN order o
ON c.customerID=o.customerID
INNER JOIN orderDetails d
ON o.orderID=d.orderID
INNER JOIN product p
ON p.productCode=l.productCode
WHERE orderDate <= '2015-03-15'
GROUP BY c.customerFN, c.customerEmail, p.productName
ORDER BY p.productName;
答案 1 :(得分:1)
JOIN
使用ON
子句执行,而不是WHERE
执行:
...
FROM customer c
INNER JOIN order o ON c.customerID=o.customerID
INNER JOIN orderDetails d ON o.orderID=d.orderID
INNER JOIN product p ON p.productCode=d.productCode
WHERE orderDate <= '2015-03-15'
...
加入后来自的WHERE
子句应该像在查询中一样使用。
除了JOIN
的问题之外,使用SUM
而没有分组也存在问题。你可能想要这样的东西:
SELECT c.customerFN, c.customerEmail, p.productName,
SUM(p.unitsinstock + p.unitsordered) AS "All Units"
FROM customer c
INNER JOIN order o ON c.customerID=o.customerID
INNER JOIN orderDetails d ON o.orderID=d.orderID
INNER JOIN product p ON p.productCode=d.productCode
WHERE orderDate <= '2015-03-15'
GROUP BY customerFN, customerEmail, productName
ORDER BY p.productName;
使用SUM
函数意味着GROUP BY
子句。选择的每个列都不属于SUM
等聚合函数的一部分,必须出现在GROUP BY
子句中。