Strange MySQL AVG() anomaly NULL values

前端 未结 5 981
小蘑菇
小蘑菇 2020-12-08 21:27

What I\'am doing :

create table sample (id INT(10) PRIMARY KEY AUTO_INCREMENT,name varchar(255),marks INT(10));

insert into sample (name,marks) VALUES(\'sam         


        
相关标签:
5条回答
  • 2020-12-08 21:47

    You can do this instead:

    SELECT SUM(marks) / COUNT(name)
    FROM sample 
    GROUP BY name;
    
    0 讨论(0)
  • 2020-12-08 21:54

    Try:

    select avg(case marks when null then 0 else marks end) from sample group by name;
    

    Or try and avoid nulls in the table ;)

    0 讨论(0)
  • 2020-12-08 22:01

    That's the normal behaviour, since NULL is not zero. How do you make the average of 5 + NULL? So MySQL is taking only the rows that can be averaged.

    Appart from the correct answers other users have already given you, you can also use the COALESCE function, which returns the first non-NULL value you specify in the list, so you can replace the NULL one with the one you like:

        SELECT AVG(COALESCE(marks,0)) FROM sample GROUP BY(name);
    
    0 讨论(0)
  • 2020-12-08 22:03
    SELECT
           avg(CASE WHEN price IS NULL THEN 0 else price END), 
           avg(CASE WHEN discount IS NULL THEN 0 else discount END), 
           avg(CASE WHEN total IS NULL THEN 0 else total END) 
    FROM pet;
    
    0 讨论(0)
  • 2020-12-08 22:05

    This is the correct behavior, because NULL is not the same as the number 0.

    Conceptually, NULL refers to an “unknown value” and as such it is treated differently from other values. That is why aggregate functions like AVG() ignore NULLs.

    AVG() calculates the average over all "known" values only. (= that are not NULL)

    From the MySQL docs:

    Unless otherwise stated, group functions ignore NULL values.

    Also, read about the concept of NULLs in Section "3.3.4.6 Working with NULL Values" of the MySQL manual.

    To get what you want, you might do

    SELECT AVG(IFNULL(marks, 0)) 
    FROM sample 
    GROUP BY name;
    

    IFNULL() returns the second argument for calculations if the value is NULL or passes through the value otherwise.


    There are more common misunderstandings regarding the concept of NULL. These are also explained in Section "5.5.3 Problems with NULL" of the manual:

    • In SQL, the NULL value is never true in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression.

      i.e.: NULL == 0 results in NULL instead of true. Also NULL == NULL results in NULL, instead of true.
    • To search for column values that are NULL, you cannot use an expr = NULL test. To look for NULL values, you must use the IS NULL test.
    • When using DISTINCT, GROUP BY, or ORDER BY, all NULL values are regarded as equal.
    • When using ORDER BY, NULL values are presented first, or last if you specify DESC to sort in descending order.
    • For some data types, MySQL handles NULL values specially. If you insert NULL into a TIMESTAMP column, the current date and time is inserted.
    • If you insert NULL into an integer or floating-point column that has the AUTO_INCREMENT attribute, the next number in the sequence is inserted.
    • A column that has a UNIQUE key defined can still contain multiple NULL values.
    0 讨论(0)
提交回复
热议问题