码迷,mamicode.com
首页 > 其他好文 > 详细

50个查询系列-第11个查询:查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

时间:2016-12-02 21:26:02      阅读:964      评论:0      收藏:0      [点我收藏+]

标签:bsp   where   core   rom   blog   uid   log   name   sel   

select distinct( tblstudent.StuId) from tblstudent ,

tblscore
where  tblscore.CourseId
 in
(
select tblscore.CourseId from tblscore where tblscore.StuId=1001 
)
and tblstudent.StuId=tblscore.StuId

  答案提供两种方法;

1:

Select DistInct st.StuId,StuName From tblStudent st
  Inner Join tblScore sc ON st.StuId=sc.StuId
   Where sc.CourseId IN (Select CourseId From tblScore Where StuId=1001)

2:

 ------嵌套子查询
 Select StuId,StuName From tblStudent
  Where StuId In
  (
   Select Distinct StuId From tblScore Where CourseId In (Select CourseId From tblScore Where StuId=1001)
  )

 

50个查询系列-第11个查询:查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

标签:bsp   where   core   rom   blog   uid   log   name   sel   

原文地址:http://www.cnblogs.com/shenxiaoquan/p/6127032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!