如果我有一个student_table如下:
original_student other_student
10123 20234
10234 20456
我还有其他包含学生班的表
Student_code Class
10123 class_001
20234 class_002
10234 class_003
20456 class_004
我的问题是关于student_table,如何找出original_student和other_student的行的总计数大于或等于2,我需要通过组合original_student和other_student进行计数。
谢谢
答案 0 :(得分:0)
select s.original_student, s.other_student, count(*)
from student_table s
inner join student_class c1 on c1.Student_code = s.original_student
inner join student_class c2 on c2.Student_code = s.other_student
group by s.original_student, s.other_student
having count(*) >= 2
答案 1 :(得分:0)
您可以使用IN(...)
加入条件通过单个联接执行此操作:
select s.original_student, s.other_student, count(distinct sc.class)
from student_table s
join student_class sc on sc.Student_code in (s.original_student, s.other_student)
group by s.original_student, s.other_student
having count(distinct sc.class) > 1
distinct
用于count()
,以防每个学生与同一个类代码相关联,这可能不应该算作两个类,而只是一个类。< / p>