Requirement: query the average score of two or more non-pass students.
Two query statements are often used:
Select name, sum (score <60), avg (score) from result group by name having sum (score <60)> = 2;
Select name, count (score <60 )! = 0) as a, avg (score) from result group by name having a> = 2;
The results of the two queries must be different. they must be the first correct one. The reason is why, now you have to think about the meaning of the count () function and the meaning of the sum function.
When the parameters in the count () function are column names, the number of times a value item is calculated.
When the parameters in the Sum () function are column names, they are used to calculate the Sum of column names, rather than the total number of values.
Pay attention to the number of count () rows: it calculates the total number of rows. Whether or not you have a value is included in the calculation range. Another point: the mysqlisam engine can easily obtain statistics on the total number of rows. Faster query speed
Summary: It is often used to count the total number of rows in actual programming. In this case, count (*) is used for multiple visibility. I rarely see the use of column names as parameters: count (. Even in this way, the original intention may be to count the number of rows. I just don't know! The form of "column name" is incorrect due to the slight difference. BitsCN.com
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.