多表的内连接

时间:2014-11-07 23:03:49

标签: sql ms-access inner-join

我无法理解如何INNER JOIN多个表

以下是我的表格

STUDENT

id       |    fname    |    surnamee    |    courseCode
-----------------------------------------------------------
11221    |    Donal    |    Nee         |    GY101
12345    |    Ann      |    Apple       |    GY350
33445    |    Jack     |    Carr        |    GY101
44554    |    Mark     |    Connor      |    GY350
55566    |    Claire   |    Gavin       |    GY350
98981    |    Marie    |    Fahy        |    GY101
99887    |    Hugh     |    Flynn       |    GY350

STUDENT_REG1

sID    |    modCode
--------------------
11221  |    CT2101
11221  |    CT2103
11221  |    CT230
11221  |    EC273
11221  |    EC275
12345  |    CT229
12345  |    CT230
12345  |    CT231
12345  |    CT237
33445  |    CT335
44554  |    CT230
98981  |    CT2101

MODULES1

code   |    modName    |    deptName
-------------------------------------
CT2101 |    OOP 1      |    IT
CT2103 |    SAD        |    IT
CT229  |  Programming1 |    IT
CT230  |    Database   |    IT
CT231  |    Pro Skills |    IT
CT335  |    OOP        |    IT
CT874  |  Programming1 |    IT
EC273  |  Maths Eco    |    Maths
EC275  |    Statistics |    Maths
ST237  |    Intro SDP  |    Maths

ROOM_BOOKING1

bookingID    |    roomCode    |    moduleCode    |    dayReq    |    timeReq
-----------------------------------------------------------------------------
1            |    AM200       |    CT230         |    Tuesday   |    14:00:00   
10           |    AM200       |    ST237         |    Tuesday   |    16:00:00
11           |    SC005       |    EC273         |    Monday    |    17:00:00
12           |    AM150       |    EC273         |    Tuesday   |    15:00:00
13           |    IT204       |    CT335         |    Monday    |    11:00:00
14           |    SC002       |    CT335         |    Wednesday |    16:00:00
2            |    AM200       |    CT230         |    Wednesday |    12:00:00
3            |    IT250       |    CT229         |    Friday    |    13:00:00
4            |    Ac005       |    CT2103        |    Monday    |    14:00:00
5            |    AM200       |    CT237         |    Monday    |    16:00:00
6            |    IT125G      |    CT2101        |    Monday    |    12:00:00
7            |    IT204       |    CT2101        |    Tuesday   |    12:00:00
8            |    AC216       |    CT2103        |    Friday    |    12:00:00
9            |    UC102       |    CT229         |    Friday    |    12:00:00

MOD_LECTURER1

mCode    |     lecturer
-----------------------
CT2101   |    Seamus
CT2103   |    Josephine
CT230    |    Josephine
CT231    |    Martina
CT231    |    Pat
CT335    |    Matthias
CT335    |    Seamus
ST237    |    John

ROOM1

rCode    |    rName    |    building    |    capacity    |    diabledAccess
----------------------------------------------------------------------------
AC216    |             |    Arts Con    |    70          |    -1
AM150    |    Tnuthail |    Arts Mil    |    150         |    -1
AM200    |    Fottrell |    Arts Mil    |    200         |    -1
ENG-2001 |             |    Engineering |    60          |    -1
ENG-2002 |             |    Engineering |    60          |    -1
IT125    |             |    IT          |    125         |    -1
IT125G   |             |    IT          |    125         |    -1
IT204    |             |    IT          |    59          |    -1
IT250    |             |    IT          |    250         |    -1
SC002    |    Larmor   |    Arts Con    |    100         |     0
SC005    |    Tyndall  |    Arts Con    |    100         |     0
UC102    |             |    Aras Uí     |    166         |     0

我需要使用INNER JOIN列出学生ID,姓名和总人数 学生每周的时间表,学生的时间超过 每周4小时

这是我到目前为止所拥有的

SELECT id, fname, surname, COUNT(*) AS "HOURS"
FROM STUDENT
INNER JOIN 
      STUDENT_REG1 ON STUDENT_REG1.sID = STUDENT.id
INNER JOIN 
      MODULES1 ON MODULES1.code =  STUDENT.id
INNER JOIN
      ROOM_BOOKING1 ON ROOM_BOOKING1.moduleCode = STUDENT.id;
GROUP BY roomCode
HAVING COUNT(*) >4;

它似乎无法工作但我不明白该怎么做,这是我得到的错误信息

 Syntax errpr (missing operator) in query expression 'STUDENT_REG1.sID = STUDENT.id
 INNER JOIN
     MODULES1 ON MODULES1.code = STUDENT.id
 INNER JOIN
     ROOM_BOOKING1 ON ROOM_BOOKING1.moduleCode = STUDENT.i'.

有人可以告诉我如何正确地写它

0 个答案:

没有答案