MySql 中聚合函數(shù)增加條件表達(dá)式的方法
Mysql 與聚合函數(shù)在一起時候where條件和having條件的過濾時機
where 在聚合之前過濾
當(dāng)一個查詢包含了聚合函數(shù)及where條件,像這樣的情況
select max(cid) from t where t.id<999
這時候會先進(jìn)行過濾,然后再聚合。先過濾出ID《999的記錄,再查找最大的cid返回。
having 在聚合之后過濾
having在分組的時候會使用,對分組結(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í)行。
下面是補充
有個需求,某張表,有個狀態(tài)字段(1:成功,2:失敗,類似這樣的),現(xiàn)要用日期分組統(tǒng)計不同狀態(tài)下的數(shù)量
先寫了個子查詢
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ù)
實戰(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中獲取一天、一周、一月時間數(shù)據(jù)的各種sql語句寫法
今天抽時間整理了一篇mysql中與天、周、月有關(guān)的時間數(shù)據(jù)的sql語句的各種寫法,部分是收集資料,全部手工整理,自己學(xué)習(xí)的同時,分享給大家,并首先默認(rèn)創(chuàng)建一個表、插入2條數(shù)據(jù),便于部分?jǐn)?shù)據(jù)的測試,其中部分名詞或函數(shù)進(jìn)行了解釋說明。直入主題2014-05-05Mysql數(shù)據(jù)庫綠色版安裝教程 解決系統(tǒng)錯誤1067的方法
這篇文章主要為大家詳細(xì)介紹了MySql數(shù)據(jù)庫綠色版安裝教程,以及系統(tǒng)錯誤1067的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08xtrabackup備份還原MySQL數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了xtrabackup備份還原MySQL數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06MySQL5.7更改密碼時出現(xiàn)ERROR 1054 (42S22)的解決方法
這篇文章主要為大家詳細(xì)介紹了MySQL5.7更改密碼時出現(xiàn)ERROR 1054 (42S22)的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10Mac安裝 mysql 數(shù)據(jù)庫總結(jié)
本文給大家分享的是如何在Mac下安裝mysql數(shù)據(jù)庫的方法,總結(jié)的很全面,有需要的小伙伴可以參考下2016-04-04MySQL 線上數(shù)據(jù)庫清理數(shù)據(jù)的方法
這篇文章主要介紹了MySQL 線上數(shù)據(jù)庫清理數(shù)據(jù)的方法,幫助大家更好的理解和學(xué)習(xí)使用MySQL,感興趣的朋友可以了解下2021-03-03Mysql刪除重復(fù)的數(shù)據(jù) Mysql數(shù)據(jù)去重復(fù)
這篇文章主要介紹了Mysql刪除重復(fù)的數(shù)據(jù) Mysql數(shù)據(jù)去重復(fù),需要的朋友可以參考下2016-08-08Mysql數(shù)據(jù)庫之sql基本語句小結(jié)
這篇文章主要介紹了Mysql數(shù)據(jù)庫之sql基本語句,結(jié)合實例形式總結(jié)分析了MySQL數(shù)據(jù)庫連接、登錄、查看以及數(shù)據(jù)庫、數(shù)據(jù)表等常見操作技巧,需要的朋友可以參考下2019-11-11