SQl 内连接查询

SQl query with inner join

我是 sql.I 的新手,正在使用 SQL 查询,我有两个表 Table 1 人有此数据:

Student_id        Name      Surname         Department 

Table 2个有这个数据:

Exam_id          Exam        Date

我的问题是我可以向表中添加什么列以将这些表连接在一起并从示例中获取所需的数据:一个参加物理考试的学生。

创建第三个 table,您可以在其中添加 Student_IdExam_Id。此 table 将充当上述两个 table 之间的桥梁 table。

因为这种情况适用于多对多关系。

例如:

一个学生可以在不同的日期参加不止一次考试,并且一次考试可以由不同的学生参加。

因此,如果您想在 StudentExam 之间建立关系,您的数据库图表将类似于上面的图表。

举个简单的例子:

学生

Student_Id  Name  SurName   Department
 1           ABC    XYZ      Science
 2           DEF    YXZ      Science

考试

Exam_Id   Exam       Date
 1        Physics    12-4-2018
 2        Chemistry  15-4-2018
 3        Biology    18-4-2018
 4        Maths      20-4-2018

现在你将如何建立这两个 table 之间的关系? 答案是你必须像这样

创建一个桥table(如前所述)

Student_Exam_link

Id  Student_ID  Exam_Id
1       1         1
2       2         1
3       1         2
4       2         2
5       1         3
6       2         4

在上面 table 第一行说: Student with id 1 ie. ABC has the exam of id 1 i.e. Physics

同样,第二行说:Student with id 2 ie. DEF has also the exam of id 1 i.e. Physics

等等。

现在,您如何加入这些 table?

在这三个 table 上一起使用 Join 以获得所需的 results.For 示例: 在 student_id 上加入 Student table 和 Student_Exam_link table,然后在 student_id 上加入结果 table 和 Exam table exam_id。

有关更多信息,请遵循以下 link :

https://fmhelp.filemaker.com/help/16/fmp/en/index.html#page/FMP_Help/many-to-many-relationships.html

One Student can takes Many Exams and also Many Students can take One Exam.

这样就形成了Many-to-Many关系。更好的方法是引入一个新的映射 table Student Exam Mapping table with columns

id   Exam_id    Student_id