Mysql conditional average (avg)

Asked

Viewed 42 times

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 at which the value of state for gaveup is not considered, by fraction be of value NULL:
how can I assign value 0,0 to fraction if the value state be it gaveup to get the average correctly?

SELECT avg(fraction) FROM table

1 answer

1


In the mysql you can use the ifnull():

SELECT AVG(IFNULL(fraction, 0)) FROM table
  • 1

    Thank you very much friend!

  • @Miguelsilva, if you solved the problem, mark as accepted ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.