Help with SQL query

0

I have this query that brings me the people who marked that they want to do a course called "courtship" but what I would like to add are also those that marked other courses, for example "Finance", "Youth". this is related to answers.preguntaid that brings the response id. In the case of this query the id respuestas.preguntaid = 1734 corresponds to the course of "courtship". I'd like to add this response ID also "1735", "1736" along with the one that is 1734 . that of 1735 corresponds to "finances", 1736 corresponds to "young people" Thanks!

select distinct 
Participantes.nombre,Participantes.apellido,Participantes.Cedula,
Participantes.Celular,Preguntas.Descripcion,Respuestas.Contenido,
cursoes.nombre curso
from respuestas, CursoPlaneadoParticipantes, CursoPlaneadoes, 
Participantes,Cursoes, Preguntas
where respuestas.preguntaid = 1734 and  FechaInicio 
BETWEEN '20180317 00:00:00' And '20180317 23:59:59' 
AND Preguntas.Descripcion = 'Noviazgo'
and CursoPlaneadoParticipantes.Id = respuestas.CursoPlaneadoParticipanteId
and CursoPlaneadoes.Id = CursoPlaneadoParticipantes.CursoPlaneadoId
and Participantes.Id = CursoPlaneadoParticipantes.ParticipanteId
and Cursoes.Id = CursoPlaneadoes.CursoId
and Respuestas.Contenido = 'SI'
    
asked by Manu Abella 09.05.2018 в 17:21
source

2 answers

1

Ok, the logical answer is that you use a IN in the codes of the question and do not filter the description, since it is not necessary.

Now, I can not answer without first making a couple of comments about your code. First of all, please stop using implicit joins (those that were deprecated more than a decade ago) and use explicit joins.

You should also get used to using alias in your tables, as this improves understanding of the code.

Finally, if you have a column of type datetime or date and you need to filter a range of dates, do not use BETWEEN (especially if it is datetime ).

Taking into account the above, the code would be the following:

SELECT DISTINCT 
            p.nombre,
            p.apellido,
            p.Cedula,
            p.Celular,
            pr.Descripcion,
            r.Contenido,
            c.nombre curso
FROM respuestas r
INNER JOIN CursoPlaneadoParticipantes cpp
    ON cpp.Id = r.CursoPlaneadoParticipanteId
INNER JOIN CursoPlaneadoes cp
    ON cp.Id = cpp.CursoPlaneadoId
INNER JOIN Participantes p
    ON p.Id = cpp.ParticipanteId
INNER JOIN Cursoes c
    ON c.Id = cp.CursoId
INNER JOIN Preguntas pr
    ON r.preguntaid = pr.preguntaid
WHERE FechaInicio >='20180317' 
AND FechaInicio < '20180318' 
AND r.Contenido = 'SI'
AND r.preguntaid IN (1734,1735,1736)
;
    
answered by 09.05.2018 в 17:32
0

Do it with an "in"

where respuestas.preguntaid in(1734,1735,1736) And '20180317 23:59:59'

This way you can search for several items that match a field

check this page link

    
answered by 09.05.2018 в 17:25