Mysql带条件的count()

有一张学生成绩表 student_course

CREATE TABLE `student_course` (
  `student_id` int(11) NOT NULL COMMENT '学生id,表student.id',
  `course_id` int(11) NOT NULL COMMENT '课程id,表course.id',
  `core` int(11) DEFAULT NULL COMMENT '分数',
  `absent_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否缺考,1-缺考/0-没有缺考',
  PRIMARY KEY (`student_id`,`course_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='学生课程关系表';
  1. 统计这张表中所有的学生总数
SELECT 
	COUNT(DISTINCT student_id) `学生总数`
FROM student_course; 
  1. 统计这张表中缺考的学生总数(只要有一门课程缺考则算缺考)
SELECT 
	COUNT(DISTINCT student_id) `缺考学生总数` 
FROM student_course 
WHERE absent_flag=1;/*缺考条件*/ 
  1. 如果要用一条sql同时统计学生总数和缺考学生总数这两个数据,那么就要用到带条件的count()
SELECT 
	 COUNT(DISTINCT IF(absent_flag=1,student_id,NULL)) `缺考学生数`
	,COUNT(DISTINCT student_id) `学生总数`	
FROM student_course; 

count(student_id)工作原理:
被count的字段student_id如果为null,则不计数,不为null时才计数,
字段前面加上distinct,则会在计数时排重,count(distinct student_id)
因此COUNT(DISTINCT IF(absent_flag=1,student_id,NULL)),里面有if函数,if函数只有在absent_flag=1时才计数(返回的是student_id,而不是null)
,absent_flag!=1是返回的null(因为null是不计数的),然后在if的外层在加上distinct则进行排重操作,最后最外层才是count()。

测试数据

insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('1','1','60','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('1','2','60','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('1','3','50','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('1','4','70','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('1','5','80','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('2','1','90','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('2','2',NULL,'1');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('2','5','100','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('2','9',NULL,'0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('2','10','85','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('3','1',NULL,'1');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('3','2','83','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('3','3','85','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('3','7','86','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('3','8','70','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('3','9','50','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('4','2','55','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('4','6','51','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('4','8','52','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('4','10','53','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('5','1','80','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('5','3','75','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('5','5',NULL,'1');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('5','7','71','0');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('5','9',NULL,'1');
insert into `student_course` (`student_id`, `course_id`, `core`, `absent_flag`) values('5','10','72','0');





原创文章 21 获赞 13 访问量 8391

猜你喜欢

转载自blog.csdn.net/iteye_19045/article/details/103300721