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
Since enabling https access for the entire site, ...
Moore's Law no longer applies Starting with F...
1. Check the synchronization status of A and B da...
Table of contents Preface The principle of browse...
Table of contents Preface 1. Get the current time...
Background Controller @RequestMapping("/getP...
1. Make sure the system has the required libaio s...
URL: http://hostname.com/contextPath/servletPath/...
1. Single table query -> update UPDATE table_n...
concept If the index contains all the data that m...
Table of contents Preface Standard sfc writing me...
KILL [CONNECTION | QUERY] processlist_id In MySQL...
Table of contents 1. forEach() 2. arr.filter() 3....
Table of contents 1. Database Overview 1.1 Develo...
Lists for organizing data After learning so many ...