【发布时间】:2020-04-15 08:39:52
【问题描述】:
我使用的是 mysql 5.7 版,我为每个产品、数量和使用许多这样的探险都有一个表格生产
+---------+-----------------+------+--------+---------+
| Product | Type_Expedition | Pack | Amount | Weight |
+---------+-----------------+------+--------+---------+
| Chicken | A | 1 | 2 | 2 |
| Beef | A | 1 | 2 | 2 |
| Lamb | B | 1 | 2 | 2 |
| Beef | B | 2 | 2 | 4 |
| Chicken | A | 3 | 2 | 6 |
| Lamb | A | 1 | 1 | 1 |
| Lamb | A | 1 | 1 | 1 |
+---------+-----------------+------+--------+---------+
type_expedition B 和 non-B(除 B 外的所有类型探险)如何计算重量和金额的总和?
我假设使用这种语法(对不起,我想使用 dbfiddle.uk 但这是错误的)
select product, type_expedition, pack, amount, weight, (sum(amount) where type_expedition = B), (sum(weight) where type_expedition = B) from my_table
预期结果
+---------------------------------------------------+---+----+
| Total amount and weight for type_expedition B | 4 | 6 |
+---------------------------------------------------+---+----+
| Total amount and weight for type_expedition NON B | 8 | 12 |
+---------------------------------------------------+---+----+
【问题讨论】:
标签: mysql mysql-select-db