如何解决需要数据库查询帮助
这是我拥有的数据库:https://imgur.com/EmQm9vx
自从我上次使用 MySQL 数据库以来已经有一段时间了,所以我有一些需要解决的问题。
首先,我需要编写一个查询,它可以显示学生的姓名和他们注册的班级。我在 WHERE 上尝试了一些东西,但我经常迷路。
解决方案:使用 JOIN 是一种方法,因为您需要连接 3 个表。
解决方法
从第一个问题开始。我们有一个多对多的关系,其中 student_class 将每个 student_id 与 class_id 链接起来,但名称存储在链接的 student 表和 class 表中。所以我们需要加入所有三个表。
SELECT student.name,class.name
FROM student
JOIN student_class
ON student.id = student_class.student_id
JOIN class
ON class.id = student_class.class_id
;
,
需要根据studentID将student表与class_registration表内联,然后根据classID将class_registration表与class表联结。
SELECT student.Name,class.ClassName
FROM student
inner join class_registration
ON student.id = class_registration.StudentId
inner join class
ON class.classid = class_registration.ClassId
;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。