从表中选择一个字段具有相同值的行

发布于 2024-10-04 04:03:32 字数 294 浏览 4 评论 0原文

我有一个包含这两个表的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

oО清风挽发oО 2024-10-11 04:03:32

您必须将学生加入到自身中:

SELECT s1.initials, s1.lastName
FROM Student s1, Student s2
WHERE s1.studentId <> s2.studentID /* Every student has the same tutor as himself */
AND s1.tutorId = s2.tutorid

如果您想输出对:

SELECT s1.initials, s1.lastName, s2.initials, s2.lastName
FROM Student s1, Student s2
WHERE s1.studentId <> s2.studentID /* Every student has the same tutor as himself */
AND s1.tutorId = s2.tutorid

要获取导师 - 学生的列表:

SELECT tutorId, GROUP_CONCAT( initials, lastName SEPARATOR ', ') 
FROM `Student` 
GROUP BY tutorId
/* to only show tutors that have more than 1 student: */
/* HAVING COUNT(studentid) > 1 */

You'll have to join students against itself:

SELECT s1.initials, s1.lastName
FROM Student s1, Student s2
WHERE s1.studentId <> s2.studentID /* Every student has the same tutor as himself */
AND s1.tutorId = s2.tutorid

If you want to output the pairs:

SELECT s1.initials, s1.lastName, s2.initials, s2.lastName
FROM Student s1, Student s2
WHERE s1.studentId <> s2.studentID /* Every student has the same tutor as himself */
AND s1.tutorId = s2.tutorid

To get a list of Tutor - Students:

SELECT tutorId, GROUP_CONCAT( initials, lastName SEPARATOR ', ') 
FROM `Student` 
GROUP BY tutorId
/* to only show tutors that have more than 1 student: */
/* HAVING COUNT(studentid) > 1 */
美人骨 2024-10-11 04:03:32

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?

物价感观 2024-10-11 04:03:32

将 Student 表连接到自身

SELECT S1.intials, S1.lastName
FROM Student S1, Student S2 
WHERE S1.tutorId = S2.tutorId 
AND S1.studentId <> S2.studentId

Join Student table to itself

SELECT S1.intials, S1.lastName
FROM Student S1, Student S2 
WHERE S1.tutorId = S2.tutorId 
AND S1.studentId <> S2.studentId
雅心素梦 2024-10-11 04:03:32

这是 SQL Server 中的查询,我确信这个想法非常接近 mySql:

 select s1.initials,s1.lastname,s2.initials,s2.lastname from students s1 inner join students s2 on s1.tutorid= s2.tutorid and s1.studentid <> s2.studentid

this is the query in SQL Server, im sure the idea is very close to mySql:

 select s1.initials,s1.lastname,s2.initials,s2.lastname from students s1 inner join students s2 on s1.tutorid= s2.tutorid and s1.studentid <> s2.studentid
混浊又暗下来 2024-10-11 04:03:32

您必须查询每个TutorId。伪代码:

for id in tutorIds
    query('SELECT intials, lastName FROM Student WHERE tutorId = '+id )

如果您想要一个包含所有实际有学生的导师的列表,请执行以下操作

SELECT tutorId FROM Student GROUP BY tutorId

You will have to make a query for every single tutorId. Pseudo-Code:

for id in tutorIds
    query('SELECT intials, lastName FROM Student WHERE tutorId = '+id )

If you wanna have a list containing all Tutors who actually have students, do a

SELECT tutorId FROM Student GROUP BY tutorId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文