Mysql中使用count加條件統(tǒng)計的實現(xiàn)示例
前言
最近發(fā)現(xiàn)在處理Mysql問題時,count()函數(shù)頻繁上鏡,常常出現(xiàn)在分組統(tǒng)計的情景下,但是有時候并不是使用group by分好組就可以直接統(tǒng)計了,比如說一個常見的需求,統(tǒng)計每個班級男生所占的比例,這種情況一般會按照班級分組,但是分組內(nèi)不但要統(tǒng)計班級的人數(shù),還要統(tǒng)計男生的人數(shù),也就是說統(tǒng)計是有條件的,之前確實沒有考慮過怎樣實心,后來查詢了資料,總結(jié)在這里,方便日后查找使用。
Mysql中count()函數(shù)的一般用法是統(tǒng)計字段非空的記錄數(shù),所以可以利用這個特點來進(jìn)行條件統(tǒng)計,注意這里如果字段是NULL就不會統(tǒng)計,但是false是會被統(tǒng)計到的,記住這一點,我們接下來看看幾種常見的條件統(tǒng)計寫法。
測試環(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)備工作
新建一個Mysql數(shù)據(jù)表a,包含id和num兩個字段
mysql> create table a(id int, num int); Query OK, 0 rows affected (0.04 sec)
插入測試數(shù)據(jù),為了看count()函數(shù)的效果,我們插入兩個空數(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)計做比較
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(*)會查詢出所有的記錄數(shù),但如果使用count(num)發(fā)現(xiàn)只有4條數(shù)據(jù),num為NULL的記錄并沒有統(tǒng)計上
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)計
count()函數(shù)中使用條件表達(dá)式加or null來實現(xiàn),作用就是當(dāng)條件不滿足時,函數(shù)變成了count(null)不會統(tǒng)計數(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á)式來實現(xiàn),當(dāng)條件滿足是表達(dá)式的值為非空,條件不滿足時表達(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á)式來實現(xiàn),當(dāng)條件滿足是表達(dá)式的結(jié)果為非空,條件不滿足時無結(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ù)實現(xiàn)條件統(tǒng)計的基礎(chǔ)是對于值為NULL的記錄不計數(shù),常用的有以下三種方式,假設(shè)統(tǒng)計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)計的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Mysql count條件統(tǒng)計內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql中自增auto_increment功能的相關(guān)設(shè)置及問題
mysql中的自增auto_increment功能相信每位phper都用過,本文就為大家分享一下mysql字段自增功能的具體查看及設(shè)置方法2012-12-12MySQL錯誤代碼1862 your password has expired的解決方法
這篇文章主要為大家詳細(xì)介紹了MySQL錯誤代碼1862 your password has expired的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08percona-toolkit之pt-kill 殺掉mysql查詢或連接的方法
本文主要描述了percona-toolkit中pt-kill的 使用實例 ,及 一些重要參數(shù)的介紹,需要的朋友可以參考下2016-04-04數(shù)據(jù)庫mysql的四種安裝方式(非常全面?。?/a>
這篇文章主要給大家介紹了關(guān)于數(shù)據(jù)庫mysql的四種安裝方式,文中通過圖文以及代碼介紹的非常詳細(xì),不論你是初學(xué)者還是有經(jīng)驗的開發(fā)者,都希望你能從這個教程中收獲知識與樂趣,需要的朋友可以參考下2024-02-02MySQL 數(shù)據(jù)庫 binLog 日志的使用操作
binlog是MySQL數(shù)據(jù)庫中的一種日志類型,它記錄了數(shù)據(jù)庫中的所有更改操作,例如插入、更新、刪除操作,本文給大家介紹MySQL 數(shù)據(jù)庫 binLog 日志的使用,感興趣的朋友一起看看吧2023-08-08