get the values 0 in mysql query

0

estimates,

I have a survey project for students in which there is a form_alumno table where students enter anonymously and fill out a form

then it is redirected to the survey, which consists of 21 questions and each question has 5 types of answers (1,2,3,4,5).

I have the following query that is to rescue the sum of the amount of each type of answer for each question and also a filter per course, year and semester:

SELECT p.id_pregunta, tr.tipo, COUNT(r.id_respuesta) as cantidad_respuestas 
                        FROM preguntas p 
                        
                        LEFT JOIN tipo_respuesta tr on p.id_pregunta = tr.id_pregunta 
                        
                        LEFT JOIN respuesta r on tr.id_tipo = r.id_tipo

                        LEFT JOIN form_alumnos f on r.id_form_alumno_fk = f.id_form_alumno 
                        
                        WHERE f.cod_curso_alumno = '342139' AND f.anio = '2016' AND f.semestre = 'Primer'
                        
                        GROUP BY p.id_pregunta, tr.tipo

throws this table:

What I need is that the tables give me the values or the table should look like this:

Id_respuesta  tipo  cantidad_respuesta
     1         1           0
     1         2           0
     1         3           0
     1         4           4
     1         5           3
     2         1           0
     2         2           0
     2         3           0
     2         4           2
     2         5           5
     |         |           |
    21         1           0
    21         2           1
    21         3           2
    21         4           2
    21         5           2

any guidance or help will be good in advance thanks

    
asked by claudia24 21.11.2017 в 01:29
source

1 answer

0

SELECT p.id_pregunta, tr.tipo, COUNT(r.id_respuesta) as cantidad_respuestas 
                        FROM preguntas p 
                        
                        LEFT JOIN tipo_respuesta tr on p.id_pregunta = tr.id_pregunta 
                        
                        LEFT JOIN respuesta r on tr.id_tipo = r.id_tipo

                        LEFT JOIN form_alumnos f on r.id_form_alumno_fk = f.id_form_alumno 
                        
                        WHERE f.cod_curso_alumno = '342139' AND f.anio = '2016' AND f.semestre = 'Primer'
                        
                        GROUP BY p.id_pregunta, tr.tipo

The solution was to add below the WHERE or cod_curso_alumno IS NULL or anio IS NULL or semester IS NULL. In case anyone ever has the same doubt.

    
answered by 21.11.2017 в 22:19