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

50个查询系列-第9个查询:查询所有课程成绩小于60分的同学的学号、姓名;

时间:2016-12-01 11:34:29      阅读:1283      评论:0      收藏:0      [点我收藏+]

标签:sub   class   ret   sel   查询   returns   color   where   name   

第一想法:

从分数表里面把小于60的学生id都查出来。

 

SELECT tblstudent.StuId,tblstudent.StuName FROM tblstudent WHERE

(
SELECT  tblscore.Score FROM tblscore WHERE tblstudent.StuId=tblscore.StuId AND tblscore.Score<60

)

然后报错:

Subquery returns more than 1 row
 是的呀。

SELECT  tblscore.Score FROM tblscore WHERE tblstudent.StuId=tblscore.StuId AND tblscore.Score<60这句话查出来的数据有好几条呢。这样的当然是不对的。

还是不知道怎么修改,于是看答案:

select tblstudent.StuId,tblstudent.StuName from tblstudent where 
tblstudent.StuId NOT IN
(
select  tblscore.StuId from tblscore where tblstudent.StuId=tblscore.StuId AND tblscore.Score>60)

 

50个查询系列-第9个查询:查询所有课程成绩小于60分的同学的学号、姓名;

标签:sub   class   ret   sel   查询   returns   color   where   name   

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

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