Suppose I have a table with 2 columns with Order ID and Student ID:
Order ID | Student ID |
-----------------------
1 | 1
1 | 2
1 | 3
2 | 1
2 | 3
3 | 1
3 | 2
4 | 1
4 | 2
4 | 3
5 | 2
5 | 3
.....
Here, it's a many-to-many relationship: one course can include many student and one student can enroll in many courses.
The question is: I want to filter courses that contains specific a specific set of student IDs. For example:
If the student ID set is
(1,2,3), then the returned course IDs should be(1,4)as only those 2 courses got all students in the set enrolled.If the student ID set is
(1,2), then the returned course IDs should be(1,3,4).If the student ID set is
(2,3), then the result should be(1,4,5).
etc.
The student ID set can be varied in size to the limit of a set in Python.
Currently, I'm querying specific courses and stored the objects into specific lists, then filter with Python. However, querying from thousands of items several times from the table above is just slow.