MySQL filtering timing of where conditions and having conditions when used with aggregate functions where filters before aggregation When a query contains aggregate functions and where conditions, such as Having filters after aggregation Having is used when grouping to filter the grouping results, and usually contains aggregate functions. SELECT ip,MAX(id) FROM app GROUP BY ip HAVING MAX(id)>=5 First group, then aggregate, and then filter the result set where the aggregate result is greater than or equal to 5 The difference between the two: where is executed first, and then the aggregate function is executed. Having is executed after the aggregate function is executed. The following is a supplement There is a requirement that a table has a status field (1: success, 2: failure, and so on). Now we need to use date grouping to count the number of items in different states. First write a subquery 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') ) The execution is quite slow, so I wondered if I could add conditions to count, and found the following: 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') Easy to understand and highly efficient in execution Other aggregate functions can also be used, such as SUM and other aggregate functions Practical example: select count(if(create_date < '2017-01-01' and host_profile_id = '9294d2bf-f457-4fe5-9a36-e5f832310dc2',true,null)) from profile_visit_log -- Equivalent to select count(if(create_date < '2017-01-01',true,null)) count from profile_visit_log where host_profile_id = '9294d2bf-f457-4fe5-9a36-e5f832310dc2' Well, this is the end of this article. I hope you will support 123WORDPRESS.COM in the future. You may also be interested in:
|
>>: Vue Element front-end application development preparation for the development environment
They are all web page templates from the foreign ...
The main text starts below. 123WORDPRESS.COM Down...
Recently, the client of a project insisted on hav...
Idea: Just sort randomly first and then group. 1....
Table of contents Preface Computed properties Int...
CentOS 8 is officially released! CentOS fully com...
Some web pages may not look large but may be very ...
Table of contents Basic usage of Promise: 1. Crea...
1. Introduction Is it considered rehashing old st...
1 What is BEM Naming Standard Bem is the abbrevia...
1. What is HTML HTML (HyperText Markup Language):...
# Installation daemon configuration for Redis on ...
Publish Over SSH Plugin Usage Before using Publis...
The Kubernetes team recently announced that it wi...
DOCTYPE Doctype is used to tell the browser which...