我想通过内部联接从更多表中选择数据。

这些是我的桌子。

Student (studentId, firstName, lastname)
Exam (examId, name, date)
Grade (gradeId, fk_studentId, fk_examId, grade)

我想写一份声明,显示学生去过哪个考试,年级和日期。日期后排序。

这是我的声明。它可以运行,但是我想确保自己做的正确。
SELECT
  student.firstname,
  student.lastname,
  exam.name,
  exam.date,
  grade.grade
FROM grade
  INNER JOIN student
    ON student.studentId = grade.gradeId
  INNER JOIN exam
    ON exam.examId = grade.gradeId
ORDER BY exam.date

最佳答案

几乎正确..查看联接,您引用的字段错误

SELECT student.firstname,
       student.lastname,
       exam.name,
       exam.date,
       grade.grade
  FROM grade
 INNER JOIN student ON student.studentId = grade.fk_studentId
 INNER JOIN exam ON exam.examId = grade.fk_examId
 ORDER BY exam.date

关于mysql - 内部连接与MySQL中的3个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16013364/

10-08 21:48