我正在尝试构建一个包含 3 个以上数据分散在其中的表的查询
I am trying to build a query with more than 3 tables where data is scattered across
Table tblSubject
subId
subName
1
Mathematics
2
English
Table tblTeachers
teacherId
teacherName
1
Jean
2
Mary
Table tblStudent
studentId
studentName
1
Edward
2
Lauren
Table tblStudentSubMap
Id
studentId
teacherId
subId
1
1
1
1
2
2
1
1
3
1
2
1
4
2
2
1
5
1
1
2
6
2
1
2
7
2
2
2
我正在尝试获得以下输出,但无法使用各种内部联接等
尝试查询:
SELECT tblsubject.subname, tblTeachers.teacherName
FROM tablesubject, tblteachers
WHERE tblStudentSubMap.studentId=1
WHERE
条件输入应为studentid
并返回相应的学科名称和教师名称。
输出
id
tablesubject
teacherName
1
Mathematics
Jean
2
English
Mary
对于 Studentid = 1
有多种连接表的方法,但您可以试试我下面的代码
//Get columnName in every Table
SELECT map.Id,s.studentName,t.teacherName,sub.subName
FROM tblStudentSubMap map
LEFT JOIN tblStudent s ON s.studentID = map.studentID//Find Data with the same ID
LEFT JOIN tblTeacher t ON t.teacherID = map.teacherID//Find Data with the same ID
LEFT JOIN tblSubject sub ON sub.ID = map.subID;//Find Data with the same ID
Table tblSubject
subId | subName |
---|---|
1 | Mathematics |
2 | English |
Table tblTeachers
teacherId | teacherName |
---|---|
1 | Jean |
2 | Mary |
Table tblStudent
studentId | studentName |
---|---|
1 | Edward |
2 | Lauren |
Table tblStudentSubMap
Id | studentId | teacherId | subId |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 | 1 | 1 |
3 | 1 | 2 | 1 |
4 | 2 | 2 | 1 |
5 | 1 | 1 | 2 |
6 | 2 | 1 | 2 |
7 | 2 | 2 | 2 |
我正在尝试获得以下输出,但无法使用各种内部联接等
尝试查询:
SELECT tblsubject.subname, tblTeachers.teacherName
FROM tablesubject, tblteachers
WHERE tblStudentSubMap.studentId=1
WHERE
条件输入应为studentid
并返回相应的学科名称和教师名称。
输出
id | tablesubject | teacherName |
---|---|---|
1 | Mathematics | Jean |
2 | English | Mary |
对于 Studentid = 1
有多种连接表的方法,但您可以试试我下面的代码
//Get columnName in every Table
SELECT map.Id,s.studentName,t.teacherName,sub.subName
FROM tblStudentSubMap map
LEFT JOIN tblStudent s ON s.studentID = map.studentID//Find Data with the same ID
LEFT JOIN tblTeacher t ON t.teacherID = map.teacherID//Find Data with the same ID
LEFT JOIN tblSubject sub ON sub.ID = map.subID;//Find Data with the same ID