数据库教程-mariadb教程-MariaDB Sum()函数

跨零代码为大家提供高品质的解决方案,请大家多多来访,跨零不胜感激,在此谢过。

MariaDB SUM()函数用于返回表达式求和的值。

语法:

SELECT SUM(aggregate_expression)   FROM tables   [WHERE conditions]; 

数据准备

"testdb"数据库中创建一个"students"表,并插入一些数据。参考以下创建语句 –

USE testdb; DROP TABLE students; -- 创建新表 CREATE TABLE students(       student_id INT NOT NULL AUTO_INCREMENT,       student_name VARCHAR(100) NOT NULL,       student_address VARCHAR(40) default NULL,      admission_date DATE,     score float(4, 1) default NULL,      PRIMARY KEY ( student_id ) ); -- 插入数据 INSERT INTO students   (student_id, student_name, student_address,score, admission_date)   VALUES(1,'Maxsu','Haikou', 99.5,'2017-01-07 00:00:00');  INSERT INTO students   (student_id, student_name, student_address, score, admission_date)   VALUES   (2,'Crurry','Beijing',86,'2016-05-07 00:00:00'), (3,'JMaster','Beijing',91,'2016-05-07 00:00:00'),   (4,'Mahesh','Guangzhou',78,'2016-06-07 00:00:00'),   (5,'Kobe','Shanghai',89,'2016-02-07 00:00:00'),   (6,'Blaba','Shengzhen',100,'2016-08-07 00:00:00'); 

1. SUM()函数与单表达式

示例:

计算student_id大于1Student表的总分数。参考以下查询语句 –

SELECT SUM(Score) AS "Total Score"   FROM students   WHERE student_id > 1; 

执行上面查询语句,得到以下结果 –

MariaDB [testdb]> SELECT SUM(Score) AS "Total Score"     -> FROM students     -> WHERE student_id > 1; +-------------+ | Total Score | +-------------+ |       444.0 | +-------------+ 1 row in set (0.00 sec) 

2. SUM()函数与DISTINCT子句

可以使用SUM函数的DISTINCT子句来避免重复值的总和。

再插入一条重复的数据 –

INSERT INTO students   (student_name, student_address,score, admission_date)   VALUES('Maxsu','Haikou', 99.5,'2017-11-07 00:00:00'); 

当前数据库中的记录如下 –

MariaDB [testdb]> select * from students; +------------+--------------+-----------------+----------------+-------+ | student_id | student_name | student_address | admission_date | score | +------------+--------------+-----------------+----------------+-------+ |          1 | Maxsu        | Haikou          | 2017-01-07     |  99.5 | |          2 | Crurry       | Beijing         | 2016-05-07     |  86.0 | |          3 | JMaster      | Beijing         | 2016-05-07     |  91.0 | |          4 | Mahesh       | Guangzhou       | 2016-06-07     |  78.0 | |          5 | Kobe         | Shanghai        | 2016-02-07     |  89.0 | |          6 | Blaba        | Shengzhen       | 2016-08-07     | 100.0 | |          7 | Maxsu        | Haikou          | 2017-11-07     |  99.5 | +------------+--------------+-----------------+----------------+-------+ 7 rows in set (0.00 sec) 

示例:

SELECT SUM(DISTINCT Score) AS "Total Score"   FROM students   WHERE student_name = 'Maxsu'; 

执行上面查询语句,得到以下结果 –

MariaDB [testdb]> SELECT SUM(DISTINCT Score) AS "Total Score"     -> FROM students     -> WHERE student_name = 'Maxsu'; +-------------+ | Total Score | +-------------+ |        99.5 | +-------------+ 1 row in set (0.00 sec) 

  

从零到一,创造未来!跨零综合IT问题解决服务站,欢迎你的到来。Oracle数据库教程 mariadb教程 只为你绽放。

本文固定链接: http://kua0.com/2019/01/31/数据库教程-mariadb教程-mariadb-sum函数/

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注