For det første mangler du en GROUP BY
klausul nederst i din forespørgsel for at gruppere efter school_name
:
SELECT count(student_name) AS total_student, school_name
FROM student
LEFT JOIN school_info ON school_info.school_id = student.school_id
WHERE student.status = '0'
GROUP BY school_name
Så, hvis du simpelthen ikke vil vise rækker, hvor total_student =0, kan du bruge MySQL HAVING-sætningen:
SELECT count(student_name) AS total_student, school_name
FROM student
LEFT JOIN school_info ON school_info.school_id = student.school_id
WHERE student.status = '0'
GROUP BY school_name
HAVING count(student_name) > 0
Eller du kan ændre LEFT JOIN
til INNER JOIN
at opnå det samme i dette tilfælde.
Endelig, hvis du i stedet vil erstatte 0 med null, men stadig har rækker, kan du opdatere select-sætningen og få totalerne til:
SELECT IF(COUNT(student_name) = 0, NULL, COUNT(student_name)) AS total_student, school_name