SET @courses_completed = '2'; SELECT c.course_id FROM course c LEFT JOIN course_prerequisites p ON p.course_id = c.course_id WHERE NOT FIND_IN_SET(c.course_id, @courses_completed) GROUP BY c.course_id HAVING SUM(COALESCE(FIND_IN_SET(p.prerequisite_course_id, @courses_completed), 0) > 0) = COUNT(p.prerequisite_course_id);
Here is what the above code is Doing:
1. It’s creating a variable called @courses_completed and setting it to ‘2’.
2. It’s selecting all courses that are not in the @courses_completed variable.
3. It’s grouping the results by course_id.
4. It’s counting the number of prerequisites for each course.
5. It’s counting the number of prerequisites that have been completed for each course.
6. It’s comparing the number of prerequisites to the number of prerequisites completed.
7. It’s returning the courses where the number of prerequisites equals the number of prerequisites completed.