简体   繁体   English

如何将多个表链接到一个查询中

[英]How to link multiple tables together into a single query

I am in need of help. 我需要帮助。 I never worked with complex databases, and I don't know how to arrange the tables. 我从来没有使用过复杂的数据库,也不知道如何安排表格。 I need a table with children names, which is linked to their parents account (did this using $_POST['children_name'] , etc when submitting the registration form), but now I need to link their schools with their grades (example I am in 8 A grade), then their classes with the individual grade school subjects, and then link marks (like A+ in America) and absences to individual children. 我需要一张带有孩子名字的表格,该表格链接到他们的父母帐户(在提交注册表时使用$_POST['children_name']等),但是现在我需要将他们的学校与他们的成绩相关联(例如,我是8年级),然后将他们的课程与各个年级的学校科目联系起来,然后将分数(例如美国的A +)和缺勤与各个孩子联系起来。

I have no idea on how to do this. 我不知道该怎么做。 I made an example, which is illustrated in the photo below. 我举了一个例子,如下图所示。 Could you please help me? 请你帮助我好吗?

(NOTE THAT THERE ARE 3 TABLES THERE) (注意那里有3张桌子)

Image 图片

LATER EDIT: Fluffeh`s solution, a little bit edited! 之后编辑:Fluffeh的解决方案,进行了一些编辑! " I would consider the following structure for your tables. “我将为您的表考虑以下结构。

First, a table for all the schools that the website will have data from. 首先,列出该网站将提供数据的所有学校的表格。

id    schoolname
 1     European College#1
 2     European Colelge#2

Then, a table of all the classes that are active in a college. 然后,列出大学中所有活动班级的表格。

id    Class
 1    8A
 2    8B
 3    8C

then a table for the subjects offered by all the schools. 然后是所有学校开设的科目表。 You will need an ID and then any other information as needed. 您将需要一个ID,然后根据需要提供任何其他信息。

    schoolSubject
id  name
1   mathematics
2   geography
    3       physics
    4       geometry

Next up a table containing relations between the schools, classes and school subjects. 接下来,创建一个表格,其中包含学校,班级和学校科目之间的关系。

relations
SchoolID   ClassID     SubjectID
1           1               1
1           1               2
1           1               4

(so, if my logic is correct, class 8A students from the "European College#1" will study mathematics, geography and geometry) (因此,如果我的逻辑是正确的,“欧洲学院#1”的8A级学生将学习数学,地理和几何)

Lastly, we will need to know which student is from where. 最后,我们将需要知道哪个学生来自哪里。

students
id  name        class_id   schoolname_id
1   John Doe     1           1
2   Jane Doe     2           2

The last tabe pretty much gives us the ability to have a record of multiple grades for the same student for the same subject - in case a student has to take a subject more than once. 最后一个选项几乎使我们能够记录同一学生针对同一科目的多个成绩-以防学生必须多次学习一门科目。

grades
subjectID   studentID   grade
1           1           B,A,B
1           2           A,B,B
2           1           A,C,C
2           2           B,A,A

With this structure in place, what query could I use? 有了这个结构,我可以使用什么查询?

I would consider the following structure for your tables. 我将为您的表考虑以下结构。

Firstly, a table for the subjects offered by the school. 首先,列出学校提供的科目。 You will need an ID and then any other information as needed. 您将需要一个ID,然后根据需要提供任何其他信息。

schoolSubject
id  name
1   mathematics
2   geography

Next up a table containing student information. 接下来,创建一个包含学生信息的表格。 Again, we have a unique ID, then any info as needed 同样,我们有一个唯一的ID,然后根据需要提供任何信息

student
id  name
1   John Doe
2   Jane Doe

The next table allows what is known as a many to many relationship. 下表允许所谓的多对多关系。 basically it means you can have multiple entries for the same subject (as many students can be enrolled in each) and multiple entries for students (as students can be enrolled in multiple subjects). 基本上,这意味着您可以为同一科目设置多个条目(因为每个科目可以注册许多学生),并且可以为学生设置多个条目(因为可以将多个学科的学生注册)。

studentEnrolment
subjectID   studentID
1           1
1           2
2           1

The last tabe pretty much gives us the ability to have a record of multiple grades for the same student for the same subject - in case a student has to take a subject more than once. 最后一个选项几乎使我们能够记录同一学生针对同一科目的多个成绩-以防学生必须多次学习一门科目。

grades
subjectID   studentID   grade
1           1           B
1           2           A
2           1           A
2           2           B

With this stucture in place, you can do a query like the following: 使用此结构后,您可以执行如下查询:

select
    subj.name,
    stud.name,
    grad.grade
from
    schoolSubject subj
        join studentEnrolment enr
            on subj.id=enr.subjectID
        join student stud
            on enr.studentID=stud.studentID
        join grades grad
            on subj.subjectID=grad.subjectID
            and stud.id=grad.studentID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM