首页 新闻 赞助 找找看

急求一条SQL语句. 求DB进来指导..

0
悬赏园豆:5 [已解决问题] 解决于 2014-06-16 16:13

我要查询出student_class中,同一个studentId,而它的classId数量大于5的student信息,  有一个student_Info表和class_info表作关系... 

数据库是mysql

飞翔的程序猴的主页 飞翔的程序猴 | 初学一级 | 园豆:92
提问于:2014-06-13 10:51
< >
分享
最佳答案
0

已经过建表 运行验证成功

select * from
  student_info where student_id in (
  select student_id from(
  select student_id,count(class_id) cnt from
  student_class group by student_id ) t where cnt>5
  );

收获园豆:2
itlong | 菜鸟二级 |园豆:224 | 2014-06-16 15:59

谢谢,问题已经解决.用的是:having筛选

select s.STUDENT_ID, s.* from student_Info s where student_Id not in
( select student_Id from student_class
group by student_Id having count(student_Id)>5)

飞翔的程序猴 | 园豆:92 (初学一级) | 2014-06-16 16:10
其他回答(3)
0

select  * from student_info where id in(

(select  student_id   from 

(   select student_id  ,count( student_id ) as cnt from student_class group by  student_id ) as t1

where t1.cnt>5)as t2);

收获园豆:1
N1900 | 园豆:203 (菜鸟二级) | 2014-06-13 11:11
0

这个简单,看你的信息说明student_id相同的时候,class_id肯定不想同,因为一个班级不可能有两个相同的student_id

所以:select student_id from 结果1 group by student_id having count(student_id) > 5

收获园豆:1
刘宏玺 | 园豆:14020 (专家六级) | 2014-06-13 11:16
0
select student_id from student_class group by student_id having count(distinct class_id)>5
收获园豆:1
飞来飞去 | 园豆:2057 (老鸟四级) | 2014-06-13 11:58

谢谢,问题已经解决. 

支持(0) 反对(0) 飞翔的程序猴 | 园豆:92 (初学一级) | 2014-06-16 16:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册