解決多對多關係的最常見方法是通過單獨的relation table
(正如Barmar指出的那樣)。
你的情況,該表可能有以下字段:
table class_students
--------------------
id // an unique id of that relationship;
// could be ommitted, could be an autoincrement,
// could also be a "handcrafted" id like classid_userid -> 21_13 (I need this kind of id's for an ember-api.
// All depending on your needs
class // the id of the related class
student // the id of the related student
// maybe add additional fields:
type // to describe that relationship
sort
然後你會得到一個特定類的所有的學生都像這樣:
$class_id = 1;
$sql = "Select * from students, class_students where students.id=class_students.student AND class_student.class=".$class_id;
// note, that you should do that via prepared statements,
// this is only for simplicity to show how to proceed.
這是完全正常使用該類型的表得到的,' class_student'在你的例子中。當你需要多對多的關係時。你可以繼續。 – abeyaz
''我如何將學生添加到班級表中?' - 嗯,你不這樣做。您可以使用它們之間的表格連接記錄,完全如您所述。有什麼問題? – David
我認爲一個更常見的術語是_relation table_。 – Barmar