SQL - 列出已注册类的成员

时间:2017-02-28 14:34:18

标签: sql

我想弄清楚为什么这不起作用。我正在尝试选择每个成员的姓名以及每个成员注册的班级以及班级培训师姓名。

这就是我现在所拥有的:

SELECT 
    firstName, lastName, className, trainerName 
FROM 
    member, _class, trainer, enrolment 
WHERE 
    enrolment.memberID = member.memberID 
    AND enrolment.classID = _class.classID;

任何帮助或提示都会受到赞赏,因为我还在学习SQL。

额外信息:

  • enrolment表包含列entrolmentID,memberID和classID
  • trainer表格包含列trainingID和trainerName
  • member表包含memberID,firstName,lastName,age,trainerID
  • class表包含classID,className,_day和trainerID

2 个答案:

答案 0 :(得分:1)

JOIN语句的有效语法是:

SELECT FIELD1
    ,...FIELDN
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON T1.FIELDX = T2.FIELDY

所以在你的情况下,使用:

SELECT firstName
    ,lastName
    ,className
    ,trainerName
FROM member
INNER JOIN enrolment ON enrolment.memberID = member.memberID
INNER JOIN _class ON enrolment.classID = _class.classID
INNER JOIN trainer ON trainer.trainerID = member.trainerID

答案 1 :(得分:0)

试试这个

SELECT firstname, lastname, classname, trainerName
FROM member
INNER JOIN enrollment 
ON enrolment.memberID = member.memberID
INNER JOIN _class
ON enrollment.classID = class.classID
INNER JOIN trainer
ON trainer.trainerID = member.trainerID