MySQL SUM 函数用于对十进制值求和
让我们首先创建一个表 −
mysql> create table DemoTable ( Money DECIMAL(7,2) ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable values(100.67); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(199.33); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(500); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(400); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(800); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+--------+ | Money | +--------+ | 100.67 | | 199.33 | | 500.00 | | 400.00 | | 800.00 | +--------+ 5 rows in set (0.00 sec)
以下是添加十进制值的查询 −
mysql> select sum(Money) from DemoTable;
这将产生以下输出 −
+------------+ | sum(Money) | +------------+ | 2000.00 | +------------+ 1 row in set (0.00 sec)
广告