我有三张桌子:
学生,模块和学生模块(这是一个链接表)。我正在尝试做的 id 选择与特定学生关联的模块。我知道这是错误的,但我想做的是这样的:
SELECT id, name
FROM modules
WHERE id = (SELECT moduleID FROM students_modules WHERE studentID = '123')
所以任何人都可以帮助我。
用于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';
尝试 IN 而不是 = 您的子查询:
SELECT id, name
FROM modules
WHERE id IN (SELECT moduleID FROM students_modules WHERE studentID = '123')