I have the following query where I am bringing all the information that is linked to the author of the course, all the rest works very well, except when counting the enrolled students COUNT (i.id_inscribed) since a condition has passed to the WHERE i.status = 'Completed' where it should only take into account the students that have the completed status.
$stmt = $con->prepare("SELECT AVG(ra.rating) AS avg_rating,
COUNT(ra.rating) AS valuations,
SUM(c.study_program='intensive') AS total_intensive,
SUM(c.study_program='proyect') AS total_proyect,
COUNT(i.id_inscribed) AS enrolled_i
FROM tbl_ratings ra
LEFT JOIN tbl_courses c ON ra.id_course = c.id_course
LEFT JOIN tbl_author a ON c.id_author = a.id_author
LEFT JOIN tbl_inscribed i ON i.id_course = c.id_course
WHERE
a.id_author=1 AND
i.status='Completed' AND
(c.study_program='intensive' OR
c.study_program='proyect')
GROUP BY a.id_author;");
Here the query and the data obtained in real time:
https://www.db-fiddle.com/f/s2uBnyTLgTXqADtVhkgEUv/2
You will notice that in the enrolled_i it shows 2 and, it should show 1 because of the type of condition passed in the WHERE. What am I doing wrong?
There is information that has nothing to do with the author of the course, the idea of this query is to obtain the total of reviews that that instructor has had and, the general evaluation of the instructor for all the votes of his courses, the total of courses of type intensive course that the instructor and project-type courses have. Now the total number of students that the instructor has is here where you only want to obtain the data with the completed status.
And, when doing this:
(c.study_program='intensive' OR c.study_program='proyect')
I'm getting bad data, to the aforementioned approach:
avg_rating valuations total_intensive total_proyect enrolled_i
5 1 1 0 1
The expected output is the following:
avg_rating valuations total_intensive total_proyect enrolled_i
4 2 1 1 1
The changes mentioned, which generate an error for me here: https://www.db-fiddle.com/f/s2uBnyTLgTXqADtVhkgEUv/3