Mysql中使用count加條件統(tǒng)計(jì)的實(shí)現(xiàn)示例
前言
最近發(fā)現(xiàn)在處理Mysql問題時(shí),count()函數(shù)頻繁上鏡,常常出現(xiàn)在分組統(tǒng)計(jì)的情景下,但是有時(shí)候并不是使用group by分好組就可以直接統(tǒng)計(jì)了,比如說一個(gè)常見的需求,統(tǒng)計(jì)每個(gè)班級(jí)男生所占的比例,這種情況一般會(huì)按照班級(jí)分組,但是分組內(nèi)不但要統(tǒng)計(jì)班級(jí)的人數(shù),還要統(tǒng)計(jì)男生的人數(shù),也就是說統(tǒng)計(jì)是有條件的,之前確實(shí)沒有考慮過怎樣實(shí)心,后來查詢了資料,總結(jié)在這里,方便日后查找使用。
Mysql中count()函數(shù)的一般用法是統(tǒng)計(jì)字段非空的記錄數(shù),所以可以利用這個(gè)特點(diǎn)來進(jìn)行條件統(tǒng)計(jì),注意這里如果字段是NULL就不會(huì)統(tǒng)計(jì),但是false是會(huì)被統(tǒng)計(jì)到的,記住這一點(diǎn),我們接下來看看幾種常見的條件統(tǒng)計(jì)寫法。
測(cè)試環(huán)境
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.
準(zhǔn)備工作
新建一個(gè)Mysql數(shù)據(jù)表a,包含id和num兩個(gè)字段
mysql> create table a(id int, num int); Query OK, 0 rows affected (0.04 sec)
插入測(cè)試數(shù)據(jù),為了看count()函數(shù)的效果,我們插入兩個(gè)空數(shù)據(jù)
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
查詢表a中的數(shù)據(jù),與后面的統(tǒng)計(jì)做比較
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)
調(diào)用count()函數(shù)看效果,如果使用count(*)會(huì)查詢出所有的記錄數(shù),但如果使用count(num)發(fā)現(xiàn)只有4條數(shù)據(jù),num為NULL的記錄并沒有統(tǒng)計(jì)上
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)
條件統(tǒng)計(jì)
count()函數(shù)中使用條件表達(dá)式加or null來實(shí)現(xiàn),作用就是當(dāng)條件不滿足時(shí),函數(shù)變成了count(null)不會(huì)統(tǒng)計(jì)數(shù)量
mysql> select count(num > 200 or null) from a; +--------------------------+ | count(num > 200 or null) | +--------------------------+ | 2 | +--------------------------+ 1 row in set (0.22 sec)
count()函數(shù)中使用if表達(dá)式來實(shí)現(xiàn),當(dāng)條件滿足是表達(dá)式的值為非空,條件不滿足時(shí)表達(dá)式值為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)
count()函數(shù)中使用case when表達(dá)式來實(shí)現(xiàn),當(dāng)條件滿足是表達(dá)式的結(jié)果為非空,條件不滿足時(shí)無結(jié)果默認(rèn)為NULL;
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)
總結(jié)
使用count()函數(shù)實(shí)現(xiàn)條件統(tǒng)計(jì)的基礎(chǔ)是對(duì)于值為NULL的記錄不計(jì)數(shù),常用的有以下三種方式,假設(shè)統(tǒng)計(jì)num大于200的記錄
- select count(num > 200 or null) from a;
- select count(if(num > 200, 1, null)) from a
- select count(case when num > 200 then 1 end) from a
到此這篇關(guān)于Mysql中使用count加條件統(tǒng)計(jì)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Mysql count條件統(tǒng)計(jì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql中自增auto_increment功能的相關(guān)設(shè)置及問題
mysql中的自增auto_increment功能相信每位phper都用過,本文就為大家分享一下mysql字段自增功能的具體查看及設(shè)置方法2012-12-12MySQL錯(cuò)誤代碼1862 your password has expired的解決方法
這篇文章主要為大家詳細(xì)介紹了MySQL錯(cuò)誤代碼1862 your password has expired的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08percona-toolkit之pt-kill 殺掉mysql查詢或連接的方法
本文主要描述了percona-toolkit中pt-kill的 使用實(shí)例 ,及 一些重要參數(shù)的介紹,需要的朋友可以參考下2016-04-04

MySQL 數(shù)據(jù)庫 binLog 日志的使用操作