简体   繁体   中英

SQL SELECT using a “join table”

  1. Table Student maps Student.student_name to Student.student_id
  2. Table Course maps Course.course_name to Course.course_id
  3. Table Enrollment maps Enrollment.student_id to Enrollment.course_id (I've heard this referred to as a join table .)

What is the SELECT statement that, given a student name, will return his list of course names? I think this may be some of it:

SELECT c.course_name FROM Course c 
INNER JOIN Enrollment e ON c.course_id = e.course_id 
...
WHERE s.student_name = 'Tom';

Beyond that, I'm clueless.

(This isn't homework, it's just a simplification of a work problem.)

SELECT c.course_name FROM Enrollment e 
INNER JOIN Course c ON c.course_id = e.course_id 
INNER JOIN Student s ON s.student_id = e.student_id 
WHERE s.student_name = 'Tom';

also

SELECT c.course_name 
FROM Enrollment e, Course c, Student s
WHERE c.course_id = e.course_id 
  AND s.student_id = e.student_id 
  AND s.student_name = 'Tom';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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