I don't recommend your current approach, because you are trying to store comma separated, unnormalized, data in the Student
table, where there is one record with a CSV list of subjects. If someone were to give you an exact answer using this design, the query would be fairly ugly, and inefficient. Instead, I recommend the following schema:
Student (ID, name) -- and maybe other student metadata
Subjects (ID, name, description)
Student_Subjects (ID, StudentID, SubjectID)
Here, we store the relationship between one student and his subjects using multiple rows in the table Student_Subjects
. By the way, Student_Subjects
is often called a "junction" or "bridge" table, because it connects two types of a data, in a clean and normalized way.
Now, if you want to persist a given student's subjects, you can use a single insert, something like:
INSERT INTO Student_Subjects (StudentID, SubjectID)
VALUES
(1, 1),
(1, 2),
(1, 3),
(1, 4);