设为首页 加入收藏

TOP

数据库子查询
2014-11-24 07:45:11 来源: 作者: 【 】 浏览:2
Tags:数据库 查询
数据库子查询
子查询就像使用普通的表一样,被当作结果集的查询语句被称为子查询。所有可以使用表的地方几乎都可以使用子查询来代替。
SELECT  *  FROM  (SELECT * FROM student  where sAge<30)  as t

只有返回且仅返回一行、一列数据的子查询才能当成单值子查询。
子查询返回的值不止一个。当子查询跟随在=、!=、<、<=、>、>=之后,或子查询用作表达式时,这种情况是不允许的。
tips:
[sql] 
select * from TblStudent   
  
where  
  
exists  
  
(  
  
  --子查询的结果,要依赖于当前父查询中当前行的tsClassid的结果。  
  
  select * from TblClass  
  
  wheret ClassName='计算机软件班' and  tClassId=TblStudent.tsClassId  
  
)  

如果子查询是多行单列的子查询,这样的子查询的结果集其实是一个集合。可以使用in关键字代替=号
select * from student where sClassId in  ( select cId from class where cName='高一一班' or cName='高二一班' )  等价于  
  
Select* from student where exists(select * from class where (cName=‘高一一班’or cName=‘高二二班’)and class.cid=student.sclassid)  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MongoDB自学笔记6---3.1 MongoDB.. 下一篇PostgreSQL学习手册(系统视图)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)