SQL query that as a result the questions have been answered 4 times in a row

1

I wanted to get a query to return the "question number" according to the following parameters:

That it meets: -What idUser = 1 -What mode? Questions? Operator of Services - That the last 4 times that has been answered has had as a value the answersRight = 1 (4 times in a row the value 1 without interleaving with 0)

Table:

What I get to is the following, but I get all those that met the concidiniones, have as value of answers Correct = 1, but what I need is that the last 4 answers have had followed the value 1. (Since in the logic of the program, it is understood that if the user answers a question 4 times in a row (value 1), he has learned the answer and does not want that question to come up again in the program).

This to which I have come, is not enough:

SELECT numeroPregunta 
FROM 'respuestasTestUsuario' 
WHERE idUsuario=1 AND modalidadPreguntas ='operarioDeServicios' 
AND respuestasCorrectas=1

Thank you very much!

    
asked by r84 09.12.2018 в 19:30
source

1 answer

1

The key is in your explanation of:

  

4 times in a row the value 1 without intercalating with 0

And the function that does that of grouping values is GROUP_CONCAT ( link )

SELECT idUsuario, numeroPregunta, GROUP_CONCAT(correcta SEPARATOR '') as respuestas    
  FROM respuestasTestUsuario 
 WHERE idUsuario = 1 AND modalidadPreguntas ='operarioDeServicios'
 GROUP BY idUsuario, numeroPregunta
HAVING respuestasCorrectas  LIKE '%1111%'
 ORDER BY idUsuario, numeroPregunta, fechaRespuesta

Or if you want it more summarized, only with what you ask in the statement:

SELECT numeroPregunta    
  FROM respuestasTestUsuario 
 WHERE idUsuario = 1 AND modalidadPreguntas ='operarioDeServicios'
 GROUP BY idUsuario, numeroPregunta
HAVING GROUP_CONCAT(correcta SEPARATOR '') LIKE '%1111%'
 ORDER BY idUsuario, numeroPregunta, fechaRespuesta
    
answered by 09.12.2018 / 23:57
source