Usage and difference between having and where statements in mysql bitsCN.com
Usage and difference of having and where statements in mysql
When writing SQL statements, we often use where statements and rarely use having. In fact, the having clause in mysql is also a set condition statement that has similarities but differences with where statements. The having clause is slower than the aggregate statement (sum, min, max, avg, count) in the query process ). the where clause is faster than the aggregate statement (sum, min, max, avg, count) in the query process ).
Simply put:
Where clause: select sum (num) as RMB from order where id> 10 // records with IDs greater than 10 can be queried before the having clause of the aggregate statement is executed: select reportsto as manager, count (*) as reports from employees group by reportsto having count (*)> 4
Take the test database as an example. having condition expression as an aggregate statement. Certainly, the having clause query process is slower than the aggregate statement.
In other words, changing having to where will result in an error. Aggregate statements are used to count grouped data.
Having is used to determine the group data again. Having is not used if you do not use these links. Simply use where.
Having is used to make up for the deficiency of where in grouping data judgment. Because the where statement is faster than the aggregate statement.
BitsCN.com