选择几列具有相同计数

时间:2021-01-13 23:57:32

标签: mysql

我想选择几个通过完全相同考试次数的不同学生。

学生表:

<头>
id 名字
1 titi
2 tutu
3 toto
4 tata
5 tete
6 nono
7 nana
8 nini

考试表

<头>
id_stu id_course
6 5
1 3
7 1
5 6
4 7
5 2
8 4
6 6
3 3
7 2
2 7
8 1
3 2
2 6
6 4

课程表:

<头>
id 名称
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h

我试过了:

select distinct st1.firstname, st2.firstname
from students st1, students st2, exams ex1, exams ex2
where st1.id = ex1.id_student 
and st2.id = ex2.id_student 
and st1.id < st2.id
group by st1.firstname, st2.firstname
having count(ex1.id_course) = count(ex2.id_course)

我想要这样的东西:

<头>
名字 名字
toto tata
tata titi
nana tete

我不知道如何解决这个问题。 感谢您的帮助。

0 个答案:

没有答案