![]() RIGHT JOIN `lectures_students` ON lectures_students.lecture_id = 27 # // you have the lecture id because that's what you clicked on to viewĪND students_table.id = lectures_students.student_id If you want all the students in a lecture (perhaps for your own perusal), you would do something like this: SELECT * FROM `students_table` ![]() Now you have a complete set of data from the lectures table, limited to the lectures the student is enrolled in. ![]() RIGHT JOIN `lectures_students` ON lectures_students.student_id = 5 # // you already have their id b/c they are logged inĪND lectures_table.id = lectures_students.lecture_id If you want to show a student every lecture they are in, you would do something like this. E.g., if a student drops out of school and their student id is deleted, why would you want their id in your join table matching them with various lectures? You probably wouldn't, so you would set them to ON DELETE CASCADE so that their entries in the join table are removed.įor lookups, you simply need to get values from your main tables based on what's in the join table. You probably don't want a non-existent id staying in your join table. The join table has 2 columns: "student_id" and "lecture_id". MySQL has a native way of doing what you want, but it is still messy and not ideal.Īs far as the CASCADE options, they would be set where you have the foreign keys: in the join table. You can put multiple ids into a "cell", but it's messy and does not leverage the value of the relational database system. but since each lecture may have many students, and each student may have many lectures, you would need a joining table as others suggested. If you have 1-to-1 relationship, you can put the foreign key (student id) right into the lecture table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |