I have a mysql db with three tables
student
student_intervention
intervention details
I’m trying to do a pivot table view that shows all the students and has columns for each intervention type totalling up the different types of intervention for each student.
So far I have
SELECT t.`first_name`, t.`last_name`, t.`student_id`,
Count(IF(t.`intervention_details_id` = 1, 1, null)) AS Intervention1,
Count(IF(t.`intervention_details_id` = 0, 1, null)) AS Intervention2
FROM (
SELECT student.`student_id`, student.`first_name`,
student.`last_name`,
`student_intervention`.`intervention_details_id`
FROM student, student_intervention
WHERE student_intervention.student_id = student.`student_id`
) t
GROUP BY t.student_id
This works but it only shows data for students who have an intervention. I want a full list of students including those without an intervention. I think I need a JOIN but cannot figure out the right one.
Can anyone help?
use
LEFT JOINinsteadif you want prepared statement