简体   繁体   English

在MySQL中联接多个表

[英]Joining multiple tables in MySQL

I have the following table structure: 我具有以下表结构:

`user`
id(PK)
name

`course`
id(PK)
classid(FK) --> class.id
teacherid(FK)  --> user.id
starttimeid(FK) -->  timetable_time.id
endtimeid(FK) -->  timetable_time.id
name

`class`
id(PK)
name

`course_timetableday`
id(PK)
timetable_dayid(FK) --> timetable_day.id

`timetable_day`
id(PK)
value

`timetable_time`
id(PK)
value

I want to show all the courses for a particular teacherid along with its classname, timetable_day.value and timetable_time.value(starttimeid and endtimetimeid). 我想显示特定teacherid所有课程,以及其类名称,timetable_day.value和timetable_time.value(starttimeid和endtimetimeid)。

I have tried the following query: 我尝试了以下查询:

SELECT `course`.*, `class`.`name`, `timetable_day`.*, `course_timetable`.*, `timetable_time`.* FROM (`course`) JOIN `class` ON `class`.`id` = `course`.`classid` JOIN `user` ON `user`.`id` = `course`.`teacherid` JOIN `course_timetable` ON `course_timetable`.`courseid` = `course`.`id` JOIN `timetable_day` ON `timetable_day`.`id` = `course_timetable`.`timetable_dayid` JOIN `timetable_time` AS tt1 ON `tt1`.`id` = `course`.`starttimeid` JOIN `timetable_time` AS tt2 ON `tt2`.`id` = `course`.`endtimeid` WHERE `user`.`id` = 0

This gives me the following error(Though, the table is present. I've tried other queries from the same table and they all work): 这给了我以下错误(尽管该表存在。我尝试了来自同一表的其他查询,它们都可以工作):

Unknown table 'timetable_time'

Because you have aliased the table `timetable_time as tt1 and again as tt2 , your select statement needs to be modified to use tt1 or tt2 , like so: 因为您已将表`timetable_time别名别名为tt1 ,再次别名为tt2 ,所以需要将select语句修改为使用tt1tt2 ,如下所示:

SELECT `course`.*, ... , tt1.* FROM ...

Like so: 像这样:

SELECT `course`.*, `class`.`name`, `timetable_day`.*, `course_timetable`.*, tt1.* 
    FROM (`course`) 
        JOIN `class` 
            ON `class`.`id` = `course`.`classid` 
        JOIN `user` 
            ON `user`.`id` = `course`.`teacherid` 
        JOIN `course_timetable` 
            ON `course_timetable`.`courseid` = `course`.`id` 
        JOIN `timetable_day` 
            ON `timetable_day`.`id` = `course_timetable`.`timetable_dayid` 
        JOIN `timetable_time` AS tt1 
            ON `tt1`.`id` = `course`.`starttimeid` 
        JOIN `timetable_time` AS tt2 
            ON `tt2`.`id` = `course`.`endtimeid` 
        WHERE `user`.`id` = $i

NOTE: 注意:

You don't need to surround table and field names in the tick marks unless they are reserved words. 除非它们是保留字,否则您无需在刻度线中加上表格和字段名称。 So, your query could read: SELECT course.*, ... 因此,您的查询可能显示为: SELECT course.*, ...

Your table structure does not match you query, there is a table 'course_timetableday' in the structure, but in your query there is a table called 'course_timetable', which apparently has a column called 'courseid'. 您的表结构与您的查询不匹配,该结构中有一个表“ course_timetableday”,但在查询中有一个名为“ course_timetable”的表,其中显然有一个名为“ courseid”的列。 I assumed the table is named 'course_timetableday'. 我假设该表名为“ course_timetableday”。 Here is the query, see remarks below: 这是查询,请参见下面的备注:

SELECT course.*, class.`name` AS classname, timetable_day.value, tt1.value AS starttime, tt2.value AS endtime 
FROM course
JOIN class ON class.id = course.classid
JOIN user ON user.id = course.teacherid
JOIN course_timetableday ON course_timetableday.courseid = course.id
JOIN timetable_day ON timetable_day.id = course_timetableday.timetable_dayid
JOIN timetable_time AS tt1 ON tt1.id = course.starttimeid
JOIN timetable_time AS tt2 ON tt2.id = course.endtimeid
WHERE user.id = 0;

I put an alias on class.name, otherwise it might overwrite course.name (depending on your client program/adapter). 我在class.name上放置了一个别名,否则它可能会覆盖course.name(取决于您的客户端程序/适配器)。 I also put an alias on tt1.value and tt2.value for the same reason. 由于相同的原因,我还在tt1.value和tt2.value上添加了别名。 If you need the IDs from the joined tables in the result, you might need to alias them also. 如果结果中需要连接表中的ID,则可能还需要为其别名。

If you get duplicate rows in the result set, check if there is really only one entry in the course_timetableday and class tables for every course. 如果结果集中出现重复的行,请检查Course_timetableday和课程表中是否确实只有一项适用于每门课程。 Otherwise you get one result row for every matching entry in those tables. 否则,这些表中的每个匹配条目都会得到一个结果行。

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

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