Avg for null with left join

0

I have two tables (answers and answer_points) related by response_id. There are answers that do not have punctuation. I need to get all the answers with your average scores. I use this query:

SELECT 'respuestas'.'id_respuesta', 'respuestas'.'texto', 'respuestas'.'fecha', 'respuestas'.'nombre', AVG('punt_respuestas'.'puntuacion') FROM 'respuestas' LEFT JOIN 'punt_respuestas' ON 'respuestas'.'id_respuesta' = 'punt_respuestas'.'id_respuesta' 

But this only returns me the answers that have punctuation. I need those that do not have it, take it as 0.

    
asked by Ana Belén Banda Sosa 19.05.2018 в 21:19
source

1 answer

0

Use the ifnull function:

SELECT 'respuestas'.'id_respuesta', 'respuestas'.'texto', 
       'respuestas'.'fecha', 'respuestas'.'nombre', 
        AVG(ifnull('punt_respuestas'.'puntuacion',0)) 
FROM 'respuestas' 
LEFT JOIN 'punt_respuestas' 
  ON 'respuestas'.'id_respuesta' = 'punt_respuestas'.'id_respuesta'
    
answered by 20.05.2018 в 01:51