简体   繁体   English

MySQL DATABASE - SQL 语句使用第三个表连接两个表

[英]MySQL DATABASE - SQL statement to join join two tables using a third table

I am a little confused with this question.我对这个问题有点困惑。 it asks for the following: "Create a single SQL statement that will display all columns from the student and professor tables. You will need to use the student_professor table to set up the joins."它要求以下内容:“创建一个 SQL 语句,该语句将显示学生和教授表中的所有列。您需要使用 student_professor 表来设置连接。”

Normally this wouldnt be difficult under the circumstances of just having 2 tables using a related column, but this is asking me to use a 3rd table?通常,在只有 2 个表使用相关列的情况下,这并不困难,但这是要求我使用第 3 个表?

These are the tables这些是桌子

professor , student , student_professor professorstudent ,学生student_professor

Columns for 'professor' “教授”栏目

ProfessorId , ProfessorProgram , PhoneNo , Age , ProfessorName ProfessorId , ProfessorProgram , PhoneNo , Age , ProfessorName

Columns for 'student' “学生”栏目

studentno , studentprogram , phoneno , age , firstname , lastname studentno , studentprogram , phoneno , age , firstname , lastname

Columns for 'student_professor' 'student_professor' 的列

student_professor_id , ProfessorId , StudentNo , Mentor student_professor_id , ProfessorId , StudentNo , Mentor

Not tested, but this should get you started:未经测试,但这应该可以帮助您入门:

SELECT s.*, p.*
FROM professor p, student s, student_professor sp
WHERE sp.professorId = p.proffesorId
AND   sp.studentNo = s.studentNo
ORDER BY p.ProfessorName, s.lastname, s.firstname

or或者

SELECT s.*, p.*
FROM professor p
JOIN student_professor sp ON sp.professorId = p.proffesorId
JOIN student s ON sp.studentNo = s.studentNo
ORDER BY p.ProfessorName, s.lastname, s.firstname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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