I have the following mysql query:
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'
It Returns:
total_student school_name
0 NULL
What I am trying to achieve is, if total_student = 0 then show no value or NULL
total_student school_name
Could you please tell me how to do it?
Thanks 🙂
First, you’re missing a
GROUP BYclause at the bottom of your query to group byschool_name:Then, if you want to simply not show rows where total_student = 0 then you can use the MySQL HAVING clause:
Or, you can change
LEFT JOINtoINNER JOINto accomplish the same thing in this case.Finally, if instead you want to replace 0 with null but still have rows, you could update the select statement getting the totals to: