在 MySQL 中對列中的值求和?


為此,使用聚合函式 SUM()。我們首先建立一個表 −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> € int
   -> );
Query OK, 0 rows affected (0.71 sec)

使用插入命令向表中插入一些記錄 −

mysql> insert into DemoTable(€) values(10);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(€) values(200);
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable(€) values(190);
Query OK, 1 row affected (0.14 sec)

使用選擇語句從表中顯示所有記錄 −

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)

更新於: 2019-7-30

295 次瀏覽

開啟你的 職業生涯

完成課程後獲得證書

開始
廣告
© . All rights reserved.