暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Mysql中使用count加条件统计

原创 AlbertS 2019-11-24
1212

版权声明: 转载请注明出处!本文采用 知识共享 署名-非商业性使用-禁止演绎 4.0 国际许可协议

前言

最近发现在处理Mysql问题时,count()函数频繁上镜,常常出现在分组统计的情景下,但是有时候并不是使用group by分好组就可以直接统计了,比如说一个常见的需求,统计每个班级男生所占的比例,这种情况一般会按照班级分组,但是分组内不但要统计班级的人数,还要统计男生的人数,也就是说统计是有条件的,之前确实没有考虑过怎样实心,后来查询了资料,总结在这里,方便日后查找使用。

Mysql中count()函数的一般用法是统计字段非空的记录数,所以可以利用这个特点来进行条件统计,注意这里如果字段是NULL就不会统计,但是false是会被统计到的,记住这一点,我们接下来看看几种常见的条件统计写法。

测试环境

Windows 10
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

准备工作

  1. 新建一个Mysql数据表a,包含idnum两个字段
mysql> create table a(id int, num int); Query OK, 0 rows affected (0.04 sec)
  1. 插入测试数据,为了看count()函数的效果,我们插入两个空数据
mysql> insert into a values (1,100),(2,200),(3,300),(4,300),(8,null),(9,null); Query OK, 6 rows affected (0.01 sec) Records: 6 Duplicates: 0 Warnings: 0
  1. 查询表a中的数据,与后面的统计做比较
mysql> select * from a; +----+------+ | id | num | +----+------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | | 4 | 300 | | 8 | NULL | | 9 | NULL | +----+------+ 6 rows in set (0.09 sec)
  1. 调用count()函数看效果,如果使用count(*)会查询出所有的记录数,但如果使用count(num)发现只有4条数据,numNULL的记录并没有统计上
mysql> select count(*) from a; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.03 sec) mysql> select count(num) from a; +------------+ | count(num) | +------------+ | 4 | +------------+ 1 row in set (0.04 sec)

条件统计

  1. count()函数中使用条件表达式加or null来实现,作用就是当条件不满足时,函数变成了count(null)不会统计数量
mysql> select count(num > 200 or null) from a; +--------------------------+ | count(num > 200 or null) | +--------------------------+ | 2 | +--------------------------+ 1 row in set (0.22 sec)
  1. count()函数中使用if表达式来实现,当条件满足是表达式的值为非空,条件不满足时表达式值为NULL;
mysql> select count(if(num > 200, 1, null)) from a; +-------------------------------+ | count(if(num > 200, 1, null)) | +-------------------------------+ | 2 | +-------------------------------+ 1 row in set (0.05 sec)
  1. count()函数中使用case when表达式来实现,当条件满足是表达式的结果为非空,条件不满足时无结果默认为NULL;
    ```sql`
    mysql> select count(case when num > 200 then 1 end) from a;
    ±--------------------------------------+
    | count(case when num > 200 then 1 end) |
    ±--------------------------------------+
    | 2 |
    ±--------------------------------------+
    1 row in set (0.07 sec)

# 总结

使用`count()`函数实现条件统计的基础是对于值为`NULL`的记录不计数,常用的有以下三种方式,假设统计`num`大于200的记录

1. `select count(num > 200 or null) from a;`
2. `select count(if(num > 200, 1, null)) from a`
3. `select count(case when num > 200 then 1 end) from a`
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论