此查询中缺少什么 'keyword'?
What is 'keyword' is missing from this query?
这是我的;
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;
当我输入这个时,数据库在第四行抛出 "missing keyword" 错误。你能告诉我我错过了什么吗
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'
...
after join 后出现的 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
子句中。
您需要使用 ON
,而不是第 5、7 和 9 行中的 WHERE
。您也在使用函数 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;
这是我的;
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;
当我输入这个时,数据库在第四行抛出 "missing keyword" 错误。你能告诉我我错过了什么吗
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'
...
after join 后出现的 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
子句中。
您需要使用 ON
,而不是第 5、7 和 9 行中的 WHERE
。您也在使用函数 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;