从表中选择一个字段具有相同值的行
我有一个包含这两个表的 MySQL 数据库:
Tutor(tutorId, initials, lastName, email, phone, office)
Student(studentId, initials, lastName, email, tutorId)
返回共享同一导师的任何学生的姓名首字母和姓氏的查询是什么?
我尝试了 SELECT intials, lastName FROM Student WHERE coachId = coachId 但这只返回所有学生的姓名。
I have a MySQL database with these two tables:
Tutor(tutorId, initials, lastName, email, phone, office)
Student(studentId, initials, lastName, email, tutorId)
What is the query to return the initials and last names of any student who share the same tutor?
I tried SELECT intials, lastName FROM Student WHERE tutorId = tutorId
but that just returns the names of all students.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须将学生加入到自身中:
如果您想输出对:
要获取导师 - 学生的列表:
You'll have to join students against itself:
If you want to output the pairs:
To get a list of Tutor - Students:
SELECT Tutor.tutorId, Student.initials, Student.lastName FROM Student INNER JOIN Tutor ON Tutor.tutorId = Student.tutorId GROUP BY coachId
这将返回(未测试,但应该)学生列表姓名首字母和姓氏按 coachId 分组。这就是你想要的吗?
SELECT Tutor.tutorId, Student.initials, Student.lastName FROM Student INNER JOIN Tutor ON Tutor.tutorId = Student.tutorId GROUP BY tutorId
This will return (not tested, but it should) a list of student initials and last names grouped by tutorId. Is that what you want?
将 Student 表连接到自身
Join Student table to itself
这是 SQL Server 中的查询,我确信这个想法非常接近 mySql:
this is the query in SQL Server, im sure the idea is very close to mySql:
您必须查询每个TutorId。伪代码:
如果您想要一个包含所有实际有学生的导师的列表,请执行以下操作
You will have to make a query for every single tutorId. Pseudo-Code:
If you wanna have a list containing all Tutors who actually have students, do a