【发布时间】:2014-07-22 19:02:17
【问题描述】:
SELECT product_id,sum(quantity) AS total,price
FROM item,product
WHERE product.id=item.product_id
GROUP BY product_id
此查询在 mysql 中完美运行,但在 oracle 11g XE 中无法运行。 上面写着:
ORA-00979: 不是 GROUP BY 表达式
【问题讨论】:
SELECT product_id,sum(quantity) AS total,price
FROM item,product
WHERE product.id=item.product_id
GROUP BY product_id
此查询在 mysql 中完美运行,但在 oracle 11g XE 中无法运行。 上面写着:
ORA-00979: 不是 GROUP BY 表达式
【问题讨论】:
我猜你要么想要
SELECT product_id, sum(quantity) AS total, sum(price) as total_price
FROM item, product
WHERE product.id = item.product_id
GROUP BY product_id;
或
SELECT product_id, sum(quantity) AS total, price
FROM item, product
WHERE product.id = item.product_id
GROUP BY product_id, price;
【讨论】:
就像其他人回答的那样,Oracle 要求您明确地GROUP BY 您选择的任何非聚合字段。如果您使用 ANSI 连接语法,您的 DBA 将是最快乐的:
SELECT product_id, sum(quantity) AS total, price
FROM item i
JOIN product p ON (i.product_id = p.id)
GROUP BY product_id, price;
【讨论】:
FULL JOIN,那么你必须 UNION 一起使用两个查询与连接 --> 丑陋的代码,除非你每行垃圾代码都得到报酬,否则不好。