简体   繁体   English

如何使包含两个父表的内部联接的SQL查询?

[英]How to make sql query containing inner join of two parents tables?

I am developing a database containing tables that describes lessons shedule(like at university or in school). 我正在开发一个数据库,该数据库包含描述课程表的表(例如在大学或学校时)。 I have studied MySQL a little time , so I can't understand fully what I have done) Here is the ERR diagram 我已经学习了一段时间MySQL,所以我无法完全理解我所做的事情。这是ERR图

在此处输入图片说明

Please , have look at group_division table.This table is created to solve that one group can be divited by half and each sub-group won't in the same audiences. 请查看group_division表。创建该表的目的是解决一个组可以被减半的问题,并且每个子组都不会位于同一受众群体中。 So , one teacher will study one sub-group. 因此,一位老师将学习一个小组。 Now , I have troubles with creating of a query to get 现在,我在创建查询时遇到麻烦

lesson_params.idLesson , lessons_params.time_lesson , 
subjects.Name , audiences.idAudience , teachers.Surname . 

As you can see there are two parent tables - shedule and group_division. 如您所见,有两个父表-shedule和group_division。 So , how to make query to request the data above? 那么,如何使查询请求上面的数据呢? It will look like 看起来像

SELECT lesson_params.idLesson , lessons_params.time_lesson , subjects.Name , 
audiences.idAudience , teachers.Surname FROM

shedule INNER JOIN group_division ON group_division.fk_idShedule = shedule.idShedule
        INNER JOIN subjects ON shedule.fk_subject = subjects.idSubject
        INNER JOIN lesson_params ON shedule.fk_lessons_params = lesson_params.time_lesson
#and,now I need match group_division foreign keys(except fk_idShedule) to its parent tables

I can get the group_division foreign key values 我可以获得group_division外键值

SELECT lesson_params.idLesson , lesson_params.time_lesson , subjects.Name , 
group_division.fk_idTeacher , group_division.fk_idAudience FROM
shedule  
    INNER JOIN group_division ON group_division.fk_idShedule = shedule.idShedule
    INNER JOIN subjects ON shedule.fk_subject = subjects.idSubject
    INNER JOIN lesson_params ON shedule.fk_lesson_params lesson_params.time_lesson;

result 结果

|idLesson | time_lesson | Name | fk_Teacher | fk_idAudience|

Any suggestions?But , please , only not remake the table structure) 有什么建议吗,但是请不要只重做表结构)

Perhaps I'm missing something, but it looks to me like you can just add two more joins: 也许我缺少了一些东西,但是在我看来,您可以再添加两个连接:

INNER JOIN teachers ON teachers.idTeacher = group_division.fk_idTeacher
INNER JOIN audiences ON audiences.idAudience = group_division.fk_idAudience

Maybe you are thinking that each INNER JOIN has to reference a column in shedule because it is the first table in the FROM clause. 也许您认为每个INNER JOIN都必须引用shedule一列,因为它是FROM子句中的第一个表。 But that's not the case. 但是事实并非如此。

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

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