欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mysql中使用count加條件統(tǒng)計的實現(xiàn)示例

 更新時間:2022年07月29日 15:09:18   作者:AlbertS  
本文主要介紹了Mysql中使用count加條件統(tǒng)計的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

最近發(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)文章

最新評論