Přesuňte teacher_students.teacher_id = 1
zkontrolujte z WHERE
klauzule ke spojení ON
doložka.
Když podmínka, která odkazuje na tabulku na pravé straně LEFT JOIN
je vložen do klauzule WHERE, LEFT JOIN je zrušen a funguje jako INNER JOIN
.
SELECT count(teacher_students.student_id) AS rcount <--- changed
, questions.id
FROM "questions"
LEFT JOIN answers
ON answers.question_id = questions.id
LEFT JOIN teacher_students
ON teacher_students.student_id = answers.student_id
AND teacher_students.teacher_id = 1
WHERE questions.test_id = 1
GROUP BY questions.id
ORDER BY questions.id