此查询缺少什么'keyword'?

时间:2015-04-25 19:13:04

标签: sql oracle keyword

这就是我所拥有的;

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;

当我输入此数据库时,数据库会在第四行引发“缺少关键字”错误。你能告诉我我失踪了吗

2 个答案:

答案 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子句中。