简体   繁体   English

从多个表的 INNER JOINS 中获取所有记录

[英]Getting all records from INNER JOINS of multiple tables

I have three tables tblCourse , tblDegree , tblStudent .我有三个表tblCoursetblDegreetblStudent I have created a course and degree relation table as like tblCourseDegreeRelation .我创建了一个课程和学位关系表,如tblCourseDegreeRelation This relational table uses foreign keys of course and degree table as like:这个关系表当然使用外键和学位表,如下所示:

The tblCourseDegreeRelation table is like: tblCourseDegreeRelation表如下:

在此处输入图像描述

The tblCourse table is like: tblCourse表如下:

在此处输入图像描述

The tblDegree table is like: tblDegree表如下:

在此处输入图像描述

The tblStudent (In this table the degree id is foreign key d_id ) table is like: tblStudent (在此表中,学位 id 是外键d_id )表如下:

在此处输入图像描述

I need to get all records including the tblStudent all record using this query:我需要使用此查询获取所有记录,包括tblStudent所有记录:

SELECT * from tbldegree d
INNER JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
INNER JOIN tblcourse c ON cdr.c_id = c.c_id
INNER JOIN tblstudent s ON d.d_id = s.d_id
ORDER BY cdr.cdr_id DESC

But this only returns the one student's record while I've two students in the database see below:但这只会返回一个学生的记录,而我在数据库中有两个学生,请参见下文:

在此处输入图像描述

How I can get all students records from the joins query?如何从联接查询中获取所有学生记录?

In your case you have all inner joins, so it will return results where both/all tables satisfies their criteria (on clause).在您的情况下,您拥有所有内部连接,因此它将返回两个/所有表都满足其条件(on 子句)的结果。

Viewing your data your student 1 => Ali has a relation with degree 1 =>BS Information Technology.查看您的学生数据 1 => Ali 与学位 1 => BS 信息技术有关系。 Further degree 1 has courses (1 => Programming, 2 => English, 5=> Mathematics , 6 => Electronics)进一步的学位 1 有课程(1 => 编程,2 => 英语,5=> 数学,6 => 电子)

So for student 1 your inner join clause works because it has data in all joined tables.因此,对于学生 1,您的内部联接子句有效,因为它在所有联接表中都有数据。

Now if we look for your student 3 => Bilal who has a relation with degree 3 => BS Mathematics, But this degree has no assigned courses that is why your student Bilal isn't returned现在,如果我们寻找您的学生 3 => 与学位 3 => BS 数学有关系的 Bilal,但该学位没有指定课程,这就是您的学生 Bilal 没有返回的原因

To get all students no matter their related degree has courses you can turn your inner joins into left join not for all tables but for tblcoursedegreerelation and tblcourse要让所有学生无论他们的相关学位是否有课程,您都可以将您的内部联接变成左联接,而不是针对所有表格,而是针对tblcoursedegreerelationtblcourse

SELECT * 
FROM tblstudent s 
INNER JOIN tbldegree d ON d.d_id = s.d_id
LEFT JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
LEFT JOIN tblcourse c ON cdr.c_id = c.c_id
ORDER BY cdr.cdr_id DESC

Demo演示

In the result set you can see following columns as null due to no association with courses在结果集中,由于与课程没有关联,您可以看到以下列为空

cdr_id, c_id, d_id, c_id, c_name, c_credit cdr_id、c_id、d_id、c_id、c_name、c_credit

Just do a Right join on tblstudent :只需在tblstudent上做一个 Right join :

SELECT * from tbldegree d
INNER JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
INNER JOIN tblcourse c ON cdr.c_id = c.c_id
RIGHT JOIN tblstudent s ON d.d_id = s.d_id
ORDER BY cdr.cdr_id DESC

EDIT This way is better:编辑这种方式更好:

SELECT c.d_id,c.d_name,c.d_fee,cdr.cdr_id,cdr_c_id,deg.c_name,deg.d_credit,a.s_id,a.s_name
FROM tblstudent a
     left join tblDegree c ON a.d_id = c.d_id
     left join tblcoursedegreerelation cdr ON cdr.d_id=c.d_id
     left join tblcourse deg on deg.c_id=cdr.c_id

ORDER BY cdr.cdr_id DESC

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

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