table1的結構和樣本數據MySQL的SUM和GROUP BY從AS柱
+----+-------------+----------+------+-------+
| ID | DESCRIPTION | QUANTITY | EACH | PRICE |
+----+-------------+----------+------+-------+
| 1 | Product 1 | 1 | 12 | 1*12 |
| 2 | Product 2 | 2 | 3 | 2* 3 |
| 3 | Prodcut 3 | NULL | 3 | |
| 4 | Product 1 | 2 | 10 | 2*10 |
| 5 | Product 3 | NULL | 7 | |
+----+-------------+----------+------+-------+
MySQL查詢:
SELECT
DESCRIPTION,
QUANTITY,
EACH,
COALESCE(QUANTITY, 1) * EACH AS PRICE
FROM table1
GROUP BY DESCRIPTION
我如何才能讓SUM爲PRICE柱和GROUP BY描述列?我不想使用UPDATE,因爲我無法更改table1中的值。
也許調整group by子句一點也使其符合SQL標準... – Shadow
感謝您的回答 – ster