Usage and difference between having and where statements in mysql _ MySQL

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.