The difference between on, where and having

Among the three clauses that can be conditional on, where, and having, on is executed first, where is second, and having is last. Sometimes the end result is the same if the sequence doesn't affect the intermediate result. However, because on filters the records that do not meet the conditions before performing statistics, it can reduce the data to be processed by intermediate operations, which should be the fastest.

 

   According to the above analysis, it can be known that where should also be faster than having, because it does sum after filtering data, so having is the slowest. But it does not mean that having is useless, because sometimes it is necessary to use having when you do not know which record meets the requirements before step 3 comes out. 

  

   On is only used when two tables are joined, so when there is one table, there is only a comparison between where and having. In the case of this single table query statistics, if the conditions to be filtered do not involve the fields to be calculated, then their results are the same, but the rushmore technique can be used where, but having cannot, the latter is slower in speed. 

  

   If you want to involve a calculated field, it means that the value of this field is uncertain before it is calculated. According to the workflow written in the previous article, the action time of where is completed before the calculation, and having is after the calculation. only works, so in this case the results of the two will be different. 

  

   In multi-table join query, on works earlier than where. The system first combines multiple tables into a temporary table according to the join conditions between each table, then filters by where, and then calculates, and then filters by having. It can be seen that in order for the filter condition to work correctly, we must first understand when the condition should work, and then decide where to put it.

Having and where are both used for filtering, having is a filtering group, and where is a filtering record

The execution process of the sql statement is: from-->where-->group by -->having --- >order by --> select;

The aggregation function is performed for the result set, but the where condition is not run after the result set is queried, so the main function is placed in the where statement, and an error will occur.

And having is different, having is to filter the result set, so I usually put the group function in having, and use having instead of where, having is generally followed by group by

 

having combined with group by:

having is called a grouping filter condition, that is to say, it is a condition required for grouping, so it must be used in conjunction with group by

That is to say, the result of aggregate function calculation can be used as a condition, because it cannot be placed in where, and can only be solved by having.

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326212547&siteId=291194637