gpt4 book ai didi

php - 解析表(mysql, php)

转载 作者:行者123 更新时间:2023-11-29 03:27:11 25 4
gpt4 key购买 nike

我有 3 个表,分别称为 class 、 student 和 class_student(resolving table) 。这是由于类(class)和学生之间的多对多关系。一个学生可以有多个类(class),一个类(class)可以有多个学生。类里面的字段是(class_id,class_start_time和class_end_time),student(student_id,student_name,student_age),class_student(student_id,class_id)。我的问题是:在类(class)内,它包括学生,如何将学生添加到类(class)表中?还是应该使用解析表?我对解析表的理解很薄弱,我不确定它的目的是什么。

谢谢大家的回答!

当我向 class student 表添加一条新记录时,是否也向 class 表添加一条新记录?

最佳答案

解决多对多关系的最常见方法是通过单独的关系表(如 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)的所有学生:

$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.

关于php - 解析表(mysql, php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34557489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com