MySql 中聚合函數(shù)增加條件表達(dá)式的方法
Mysql 與聚合函數(shù)在一起時(shí)候where條件和having條件的過濾時(shí)機(jī)
where 在聚合之前過濾
當(dāng)一個(gè)查詢包含了聚合函數(shù)及where條件,像這樣的情況
select max(cid) from t where t.id<999
這時(shí)候會先進(jìn)行過濾,然后再聚合。先過濾出ID《999的記錄,再查找最大的cid返回。
having 在聚合之后過濾
having在分組的時(shí)候會使用,對分組結(jié)果進(jìn)行過濾,通常里面包含聚合函數(shù)。
SELECT ip,MAX(id) FROM app GROUP BY ip HAVING MAX(id)>=5
先分組,再聚合,然后過濾聚合結(jié)果大于等于5的結(jié)果集
二者的區(qū)別:
where是先執(zhí)行,然后再執(zhí)行聚合函數(shù)。having是在聚合函數(shù)執(zhí)行完之后再執(zhí)行。
下面是補(bǔ)充
有個(gè)需求,某張表,有個(gè)狀態(tài)字段(1:成功,2:失敗,類似這樣的),現(xiàn)要用日期分組統(tǒng)計(jì)不同狀態(tài)下的數(shù)量
先寫了個(gè)子查詢
select aa.logDate,aa.totalLogs ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=1) pendingLogs ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=2) successLogs ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=3) errorLogs ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=4) callbackErrorLogs from ( select DATE_FORMAT( a.startTime, '%Y-%m-%d') logDate, count(1) totalLogs from dxp.dxp_handlermodel a group by DATE_FORMAT( a.startTime, '%Y-%m-%d') ) aa
執(zhí)行相當(dāng)慢,想到count中能不能加條件,找了一下,如下:
select DATE_FORMAT( startTime, '%Y-%m-%d') logDate, count(1) totalLogs, count(if(executeStatus=1,true,null)) pendingLogs, count(if(executeStatus=2,true,null)) successLogs, count(if(executeStatus=3,true,null)) errorLogs, count(if(executeStatus=4,true,null)) callbackErrorLogs from dxp.dxp_handlermodel group by DATE_FORMAT( startTime, '%Y-%m-%d')
簡明易懂,且執(zhí)行效率非常高

其它的聚合函數(shù)也可以用,如SUM等其他聚合函數(shù)
實(shí)戰(zhàn)示例:
select count(if(create_date < '2017-01-01' and host_profile_id = '9294d2bf-f457-4fe5-9a36-e5f832310dc2',true,null)) from profile_visit_log -- 等同于 select count(if(create_date < '2017-01-01',true,null)) count from profile_visit_log where host_profile_id = '9294d2bf-f457-4fe5-9a36-e5f832310dc2'
好了這篇文章就介紹到這,希望大家以后多多支持腳本之家。
相關(guān)文章
Mysql提升索引效率優(yōu)化的八種方法總結(jié)
索引實(shí)際上也是一張表,保存了主鍵和索引的字段,并且指向?qū)嶓w表的記錄,所以索引也是需要占用空間的,這篇文章主要給大家介紹了關(guān)于Mysql提升索引效率優(yōu)化的八種方法,需要的朋友可以參考下2024-04-04
MySql 錯(cuò)誤Incorrect string value for column
能使用中文進(jìn)行搜索,但是insert into 中文是可以的。我的數(shù)據(jù)庫和數(shù)據(jù)表中所有的charset都是設(shè)置的utf8。2010-12-12
使用YUM在Linux(CentOS 7)下安裝mysql 5.7.18的教程詳解
這篇文章主要介紹了使用YUM在Linux(CentOS 7)下安裝mysql 5.7.18的教程詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05
mybatis分頁插件pageHelper詳解及簡單實(shí)例
這篇文章主要介紹了mybatis分頁插件pageHelper詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
一文教你快速學(xué)會使用DDL對數(shù)據(jù)庫和表的操作
SQL是一種操作關(guān)系型數(shù)據(jù)庫的結(jié)構(gòu)化查詢語言,今天這篇文章將詳細(xì)講述數(shù)據(jù)定義語言DDL對數(shù)據(jù)庫和表的相關(guān)操作,有感興趣的同學(xué)跟著小編一起來學(xué)習(xí)吧2023-07-07
Mysql中校對集utf8_unicode_ci與utf8_general_ci的區(qū)別說明
一直對utf8_unicode_ci與utf8_general_ci這2個(gè)校對集很迷惑,今天查了手冊有了點(diǎn)眉目。不過對中文字符集來說采用utf8_unicode_ci與utf8_general_ci時(shí)有何區(qū)別還是不清楚2012-03-03
mysql 隊(duì)列 實(shí)現(xiàn)并發(fā)讀
隊(duì)列是常用的數(shù)據(jù)結(jié)構(gòu),基本特點(diǎn)就是先入先出,在事務(wù)處理等方面都要用到它,有的時(shí)候是帶有優(yōu)先級的隊(duì)列。當(dāng)隊(duì)列存在并發(fā)訪問的時(shí)候,比如多線程情況下,就需要鎖機(jī)制來保證隊(duì)列中的同一個(gè)元素不被多次獲取2012-04-04

