I want to count the last records when they meet that:
estado
equals 1 usuario_id
equals 5, and test_id
equals 1 The maximum number of records to count is only 3.
These records I have in a table with the following structure:
I tried count () and limit like this:
public function cuenta($usuarioID,$testID){
$consulta = "SELECT COUNT(*) AS total
FROM (SELECT 1
FROM repuestas
WHERE estado = 1
AND usuario_id = '$usuarioID'
AND test_id = '$testID'
ORDER BY id DESC
LIMIT 3,3) t";
$resultado = $this->_db->query($consulta) or die(print_r($this->_db->errorInfo()));
return $resultado->fetch(PDO::FETCH_ASSOC);
}
The problem with this query is that it counts all those who have been in 1, and that would give 3 (for limit 3).
But if you look at the image in the last 3 records only 44 has been in 1 , and that should throw me:
that among the last 3, only 1 has been in 1.