Mean (avg) conditional MySQL

0

I have the following table

userid--state-----fraction  
589-----wrong---0,0  
589-----wright---1,0  
589-----wright---1,0  
589-----wrong---0,0  
589-----wrong---0,0  
589-----wrong---0,0  
589-----wrong---0,0  
589-----wrong---0,0  
589-----wrong---0,0  
589-----gaveup--NULL

When trying to bring the average using the query below the line where the state value is gaveup is not considered, by fraction has value NULL :
How can I assign value 0,0 to fraction if the value state is gaveup to get the mean correctly?

SELECT avg(fraction) FROM table
    
asked by anonymous 05.04.2018 / 19:38

1 answer

1

In mysql you can use ifnull () :

SELECT AVG(IFNULL(fraction, 0)) FROM table
    
05.04.2018 / 19:40