Processing of NULL values using aggregate functions in MySQL

Source: Internet
Author: User

Usually because of the database study is not deep, so in the interview asked some of the usual encountered problems incredibly can not be very sure to answer, really let oneself very angry!

One of the things that impressed me this time is:

when using aggregate functions in MySQL such as AVG (t), T is a field of type int in the table, nullable, with three rows of data, and the data in three rows are: 10,null,20, what is the final result?

My answer is 15, I tried it after the interview, but I was wrong about the handling of NULL on the aggregation function, and I think it was the aggregate function that would treat the null value as 0, and found that the blog was ignoring the null value because no description of the official documentation was found. Had to first agree with the blog of the cattle people said! If someone finds out, please give me an address, thank you!

Here is the result diagram of your own attempt ( note the Count function, which has a special case):

Avg Function:

Figure A

Figure II

count Function: Use COUNT (*)

Might

Figure Four

count Function: Use COUNT (xx), even with a specific field name

Figure Five

Figure Six

After your own testing, in the MySQL 5.6 environment in addition to count (*) as a special case ( do not ignore null values ), the rest of the aggregate function is really ignoring the null value (if Daniel is correct)!

Processing of NULL values using aggregate functions in MySQL

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.