table1结构和样本数据
+----+-------------+----------+------+-------+
| 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
如何为 PRICE 列和 GROUP BY DESCRIPTION 列进行 SUM ?我不想使用UPDATE,因为我无法更改table1中的值。
答案 0 :(得分:2)
那么为什么你不能在选择列表中添加SUM(COALESCE(QUANTITY, 1) * EACH) AS PRICETOTAL
。
答案 1 :(得分:0)
SELECT
DESCRIPTION,
SUM(COALESCE(QUANTITY,1)*each) as 'Price'
FROM table1
GROUP BY DESCRIPTION
returns
DESCRIPTION Price
Product 1 32
Product 2 6
Product 3 10