mysql带条件的计数
在网站开发的过程中,经常会用到数据统计功能,因此条件计数查询便是不可避免的,下面介绍几种方法来解决此问题。
例(假设):
mysql> select * from count_demo;
+-------+------+-----+
| class | pass | sex |
+-------+------+-----+
| 2 | 1 | 男 |
| 2 | 0 | 女 |
| 1 | 1 | 男 |
| 3 | 1 | 女 |
| 5 | 1 | 男 |
| 4 | 0 | 女 |
| 1 | 0 | 女 |
| 2 | 1 | 男 |
| 6 | 0 | 男 |
+-------+------+-----+ 9 rows in set
现有统计一个年级某项考试通过记录表如上(class代表班级,pass表示考试是否通过,1为通过,0为未通过,sex为性别)。
现在需要进行统计,每一个班级通过考试以及没有通过考试的人数。
方法一:使用OR NULL
mysql> select count(pass=1 or null) as pass,count(pass=0 or null) as no_pass,class from count_demo group by class;
+------+---------+-------+
| pass | no_pass | class |
+------+---------+-------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 1 | 0 | 3 |
| 0 | 1 | 4 |
| 1 | 0 | 5 |
| 0 | 1 | 6 |
+------+---------+-------+
6 rows in set
其中需要注意的是“or null”的使用,手册中提到 COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入),那么如果pass不等于1的时候,or null就起作用,让条件为null,那么count就不会计算pass不为1的值。也即是正确的统计结果。(可自行去掉or null测试一下,得到的结果是错误的)
方法二:使用多条select
mysql> select count(pass) as pass,class from count_demo where pass=1 group by class;
+------+-------+
| pass | class |
+------+-------+
| 1 | 1 |
| 2 | 2 |
| 1 | 3 |
| 1 | 5 |
+------+-------+
4 rows in set
mysql> select count(pass) as pass,class from count_demo where pass=0 group by class;
+------+-------+
| pass | class |
+------+-------+
| 1 | 1 |
| 1 | 2 |
| 1 | 4 |
| 1 | 6 |
+------+-------+
4 rows in set
这样很直观也很简单,如果统计条件少,建议使用。
方法三:使用CASE WHEN
mysql> select count(case when pass=1 then 1 else null end)as pass,count(case when pass=0 then 1 else null end)as no_pass,class
from count_demo group by class; +------+---------+-------+
| pass | no_pass | class |
+------+---------+-------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 1 | 0 | 3 |
| 0 | 1 | 4 |
| 1 | 0 | 5 |
| 0 | 1 | 6 |
+------+---------+-------+
6 rows in set
CASE WHEN语句的功能很强大,可以定义灵活的查询条件,很适合进行分类统计。
mysql带条件的计数的更多相关文章
- mysql 带条件的sum/count 使用技巧
本测试只是简单测试,其用途不在于代替count函数配合where函数进行聚合计算,而是在复杂查询中在sum/count中加入条件,一次性求出不同条件下的聚合结果. 1.插入测试数据如图 2.sum计算 ...
- mysq带条件的分页查询数据结果错误
记一次mysql分页条件查询的结果出错: 以一张用户表为例,首先我们看表中的所有数据,注意红色框住的部分: 我们使用不带条件的分页查询来查询,数据显示是OK的: SELECT id,login_nam ...
- mysql 带外注入
带外通道 有时候注入发现并没有回显,也不能利用时间盲注,那么就可以利用带外通道,也就是利用其他协议或者渠道,如http请求.DNS解析.SMB服务等将数据带出. payload SELECT LOAD ...
- springboot+thymeleaf+pageHelper带条件分页查询
html层 <div> <a class="num"><b th:text="'共 '+ ${result.resultMap['pages ...
- Excel带条件求和——SUMIF函数
老婆求帮忙,问Excel中怎么跨Sheet带条件求和,就是关于sheet2中筛选出来的数据自动合计在sheet3中 . 比如有个sheet2表中的数据如下: 现在要在sheet3中求合计, 通过分析可 ...
- Mariadb MySQL逻辑条件判断相关语句、函数使用举例介绍
MySQL逻辑条件判断相关语句.函数使用举例介绍 By:授客 QQ:1033553122 1. IFNULL函数介绍 IFNULL(expr1, expr2) 说明:假如expr1 不为NULL,则 ...
- laravel带条件查询手动分页
后台php代码: //手动分页 $users = $kaoqin; //打算输出的数组,二维 $perPage = 10; if ($request->has('page')) { $curre ...
- laravel 带条件的分页查询
laravel 带条件的分页查询, 原文:http://blog.csdn.net/u011020900/article/details/52369094 bug:断点查询,点击分页,查询条件消失. ...
- thinkphp 带条件分页查询
thinkphp 带条件分页查询:form表单传值时候,method='get'. 用 get 传值
随机推荐
- Myeclipse异常
打不开文件 问题描述:Myeclipse然打开什么东西都报错了:Could not open the editor: Invalid thread access 解决方法:1.cmd 2.cd 进入你 ...
- HTML备忘
a标签事件 a:link {color: #000000} /* 未访问的链接 */ a:visited {color: #d90a81} /* 已访问的链接 */ a:hover {color: # ...
- OpenCV之XML和YAML文件读写
FileStorage类 该类有两个构造函数 FileStorage::FileStorage() FileStorage::FileStorage(const string& source, ...
- Apache Kafka(六)- High Throughput Producer
High Throughput Producer 在有大量消息需要发送的情况下,默认的Kafka Producer配置可能无法达到一个可观的的吞吐.在这种情况下,我们可以考虑调整两个方面,以提高Pro ...
- 等差数列Arithmetic Progressions题解(USACO1.4)
Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...
- Go网络编程UDP
package main import ( "fmt" "net" "strings" ) // UDP server func main( ...
- selenium的鼠标事件操作
自动化测试过程中,经常会用到鼠标事件,在selenium的action_chains模块的ActionChains定义了鼠标操作的一些事件,要使用ActionChains类中的方法,首先需要对Acti ...
- Oracle 11G在用EXP 导入、导出时,若有空表对导入导出中遇到的问题的解决
11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segmen ...
- makecert生成证书
是要命令 makecert -r -pe -n "cn=musetowc" -$ commercial -a sha1 -b 01/01/2020 -e 01/01/2050 -c ...
- The entity type XXX is not part of the model for the current context.
今天遇到了一个奇葩问题,虽然解决了,但还是一脸懵,先附赠一下别人的解决方案:https://www.cnblogs.com/zwjaaron/archive/2012/06/08/2541430.ht ...