为此,请使用聚合函数SUM()
。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> € int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(€) values(10); mysql> insert into DemoTable(€) values(200); mysql> insert into DemoTable(€) values(190);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+------+ | Id | € | +----+------+ | 1 | 10 | | 2 | 200 | | 3 | 190 | +----+------+ 3 rows in set (0.00 sec)
以下是在MySQL中获取总和的查询-
mysql> select sum(€) AS Total from DemoTable;
+-------+ | Total | +-------+ | 400 | +-------+ 1 row in set (0.00 sec)