【发布时间】:2021-02-05 07:29:44
【问题描述】:
我将这些值放在一个表格中
我的表
+------------------+----------+-------- ---------+ |字段1 |字段2 |字段3 | +------------------+----------+-------- ---------+ | 1 | 10 | 0 | | 1 | 20 | 0 | | 2 | 100 | 0 | | 2 | 200 | 0 | | 3 | | 1 | | 3 | | 2 | | 4 | | 3 | +------------------+----------+-------- ---------|我想创建一个视图,让我得到这个结果
myview 想要的结果
+------------------+----------+-------- ---------+ |字段1 |字段2 |字段3 | +------------------+----------+-------- ---------+ | 1 | 10 | 0 | | 1 | 20 | 0 | | 2 | 100 | 0 | | 2 | 200 | 0 | | 3 | 30 | 1 | | 3 | 300 | 2 | | 4 | 330 | 3 | +------------------+----------+-------- ---------|30 is the sum of field2 where field1=1 (value of field3)
300 is the sum of field2 where field1=2 (value of field3)
330 is the sum of field2 where field1=3 (value of field3)
如何获得 30,300 和 330 的值?
【问题讨论】:
-
精确的 MySQL 版本?
-
我会避免为此构建视图。它可能没有任何用处。
标签: mysql sql sum subquery sql-view