1

如果我有一个 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来计算。

谢谢

4

2 回答 2

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
于 2013-11-11T09:01:34.060 回答
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()每个学生都与相同的班级代码相关联,这(可能)不应该算作两个班级,而应该算作一个班级。

于 2013-11-11T10:13:14.527 回答