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',10);
insert into sample (name,marks) VALUES('sam',20);
insert into sample (name,marks) VALUES('sam',NULL);
insert into sample (name,marks) VALUES('sam',NULL);
insert into sample (name,marks) VALUES('sam',30);
select AVG(marks) from sample GROUP BY(name);
OUTPUT I EXPECTED :
AVG = (10+20+30)/5 = 12
OUTPUT OF MYSQL :
AVG = (10+20+30)/3 = 20
Ideally what i wanted is that MYSQL should get the sum of 5 rows and divide it by 5 , but it only divides by 3 (the non-NULL rows)
Why does this occur and what can i do to get the correct AVG ie 60/5 ?
PS: I cannot make the marks field NOT NULL , in my db design the marks field is allowed to be NULL.
Thank you
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…