I have a column in my table called student_id, and I am storing the student IDs associated with a particular record in that column, delimited with a | character. Here are a couple sample entries of the data in that column:
243|244|245
245|1013|289|1012
549|1097|1098|245|1099
I need to write a SQL query that will return records that have a student_id of `245. Any help will be greatly appreciated.
Don’t store multiple values in the
student_idfield, as having exactly one value for each row and column intersection is a requirement of First Normal Form. This is a Good Thing for many reasons, but an obvious one is that it resolves having to deal with cases like having astudent_idof “1245“.Instead, it would be much better to have a separate table for storing the student IDs associated with the records in this table. For example (you’d want to add proper constraints to this table definition as well),
And then you could query using a join:
Note that since you didn’t post any schema details regarding your original table other than that it contains a
student_idfield, I’m calling itmytablefor the purpose of this example (and assuming it has a primary key field calledid— having a primary key is another requirement of 1NF).