3 回答

TA貢獻2003條經(jīng)驗 獲得超2個贊
SELECT
p.product_name,
SUM(o.quantity) AS quantity,
SUM(o.quantity * p.product_price) AS price
FROM products p
INNER JOIN orders o
ON p.product_id = o.product_id
GROUP BY
p.product_name
以上應該足以產(chǎn)生結(jié)果
roduct_name, quantity, price
apple 5 50
pineapple 6 45

TA貢獻1784條經(jīng)驗 獲得超7個贊
首先在表中聚合,orders然后加入產(chǎn)品:
SELECT
p.product_id,
p.product_name,
o.quantity,
o.quantity * p.product_price AS price
FROM products p INNER JOIN(
SELECT product_id, SUM(quantity) AS quantity
FROM orders
GROUP BY product_id
) o ON o.product_id = p.product_id
請參閱演示。
結(jié)果:
| product_id | product_name | quantity | price |
| ---------- | ------------ | -------- | ----- |
| 1 | apple | 5 | 50 |
| 3 | pineapple | 6 | 45 |

TA貢獻1836條經(jīng)驗 獲得超4個贊
不需要子查詢...
SELECT p.product_id, p.product_name
, SUM(o.quantity)
, SUM(o.quantity) * p.product_price AS price
FROM products AS p
INNER JOIN orders AS o
ON p.product_id = o.product_id
GROUP BY p.product_id, p.product_name, p.price
;
- 3 回答
- 0 關注
- 190 瀏覽
添加回答
舉報