在 MySQL 中结合 SUM 与 FORMAT 来格式化结果
mysqlmysqli database
让我们创建一个表 −
mysql> create table DemoTable1950 ( Amount float ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1950 values(45.60); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1950 values(101.78); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1950 values(75.90); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1950 values(89.45); Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1950;
这将产生以下输出 −
+--------+ | Amount | +--------+ | 45.6 | | 101.78 | | 75.9 | | 89.45 | +--------+ 4 rows in set (0.00 sec)
以下是在 MySQL 中组合 SUM 和 FORMAT 的查询:
mysql> select format(sum(Amount),2) as TotalSum from DemoTable1950;
这将产生以下输出 −
+----------+ | TotalSum | +----------+ | 312.73 | +----------+ 1 row in set (0.00 sec)