我有三张桌子:
学生,模块和students_modules(这是一个链接表)。我想做的是选择与特定学生相关的模块。我知道这是错的,但我尝试做的事情是这样的:
SELECT id, name
FROM modules
WHERE id = (SELECT moduleID FROM students_modules WHERE studentID = '123')
任何人都可以帮助我。
答案 0 :(得分:3)
使用JOIN
将三个表彼此链接:
SELECT
s.StudentName,
m.Id,
m.Name AS ModuleName
FROM modules AS m
INNER JOIN students_modules AS sm ON m.id = sm.moduleID
INNER JOIN students AS s ON sm.studentID = s.id
WHERE WHERE ms.studentID = '123';
答案 1 :(得分:1)
为子查询尝试IN而不是=:
SELECT id, name
FROM modules
WHERE id IN (SELECT moduleID FROM students_modules WHERE studentID = '123')