MySQL commonly used aggregate functions [learning records]

foreword

Organize and review the MySQL database, record learning, covering more complete!


1. Aggregate functions

Preparation : t_grade table, the code is as follows

-- ----------------------------
-- Table structure for t_grade
-- ----------------------------
DROP TABLE IF EXISTS `t_grade`;
CREATE TABLE `t_grade` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `stuName` varchar(60) DEFAULT NULL,
  `course` varchar(60) DEFAULT NULL,
  `score` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_grade
-- ----------------------------
INSERT INTO `t_grade` VALUES ('1', '张三', '语文', '91');
INSERT INTO `t_grade` VALUES ('2', '张三', '数学', '90');
INSERT INTO `t_grade` VALUES ('3', '张三', '英语', '87');
INSERT INTO `t_grade` VALUES ('4', '李四', '语文', '79');
INSERT INTO `t_grade` VALUES ('5', '李四', '数学', '95');
INSERT INTO `t_grade` VALUES ('6', '李四', '英语', '80');
INSERT INTO `t_grade` VALUES ('7', '王五', '语文', '77');
INSERT INTO `t_grade` VALUES ('8', '王五', '数学', '81');
INSERT INTO `t_grade` VALUES ('9', '王五', '英语', '89');

insert image description here

1.1 COUNT function

1. The COUNT() function is used to count the number of records.

-- 一般写法
select count(*) from t_grade;
-- 或者加个别名
select count(*) "成绩表总记录数" from t_grade;

insert image description here

2. Used with the GROUP BY keyword;

-- 根据学生姓名分组,显示学生姓名和每个学生上了几门课
select stuName,COUNT(course) from t_grade group by stuName;

insert image description here

1.2 SUM summation function

1. Basic usage

-- 查询张三的课程总成绩
select stuName,sum(score) from t_grade where stuName = "张三";

insert image description here

-- 查询张三的总成绩和课程
select stuName,group_concat(course).sum(score) from t_grade where stuName = "张三";

insert image description here

2. Combine with the GROUP BY keyword

-- 查询学生的课程和课程总成绩
select stuName,group_concat(course),sum(score) from t_grade group by stuName;

insert image description here

1.3 AVG function

1. Basic usage

-- 查询张三的课程平均成绩
select stuName,avg(score) from t_grade where stuName = "张三";

insert image description here

2. Combine with the GROUP BY keyword

-- 根据学生姓名分组,显示平均分
select stuName,avg(score) from t_grade group by stuName;

insert image description here

1.4 MAX function

1. Basic usage

-- 查询张三考的最高分
select max(score) from t_grade where stuName = "张三";

insert image description here

-- 将张三的最高分作为子查询返回的结果当做条件,
-- 查询张三最高分对应的的所有信息
select * from t_grade where stuName = "张三" and score = 
(select max(score) from t_grade where stuName = "张三");

insert image description here

2. Combine with the GROUP BY keyword

-- 查询每个学生的课程最高分
select stuName,max(score) from t_grade group by stuName;

insert image description here

1.5 MIN function

1. Basic usage

-- 查询张三课程的最低分
select min(score) from t_grade where stuName = "张三";

insert image description here

2. Combine with the GROUP BY keyword

-- 查询每个人课程的最低分
select stuName,min(score) from t_grade group by stuName;

insert image description here


Finish

The above is all the content, welcome to discuss, record learning!

Guess you like

Origin blog.csdn.net/m0_59420288/article/details/128783578