您好我有1个数据库,包含以下3个表:
Classroom ///
Student ///
ClassroomToStudent
学生可以有多个教室 教室可以有多个学生
Classroom:
classroomid (primary key)
classroomnumber
Student:
studentid (primary key)
studentName
ClassroomToStudent:
ctsid (primary key)
studentid
classroomid
一名叫idhn为1的约翰的学生,以及一名id为1的教室。 ClassroomToStudent表的ctsid为1,studentid为1,教室id为1。
这些是相关的。 因此,既然ClassroomToStudent表有1行,包含3个值,我想显示与教室名称相匹配的学生姓名。所以john有一个数量为10的教室。我想在HTML表格中显示这个,但我不知道要使用什么连接。我希望有人可以帮我解决这个问题。
答案 0 :(得分:1)
SELECT s.studentName, c.classroomnumber
FROM Student AS s
LEFT JOIN ClassroomToStudent AS cs
ON s.studentid = cs.studentid
LEFT JOIN Classroom AS c
ON c.classroomid = cs.classroomid
尝试一下,然后循环查询(while ($databaseClass->fetch($query)
),同时按照您的意愿回显结果:)
答案 1 :(得分:0)
SELECT s.studentName
FROM Classroom AS c
LEFT JOIN ClasroomToStudent AS cs
ON c.classroomid = cs.classroomid
LEFT JOIN Student AS s
ON cs.studentid = s.studentid
WHERE c.classroomnumber = x // x is a class number you're looking for
此sql为您的学生提供与数字x
的班级相关联的名称