sql中的子查询

在数据库的查询过程中,可能经常要用到子查询的情况,用得较多的可能是嵌套子查询,以下是我总结的子查询的一些应用
子查询基本分类:独立子查询和相关子查询
独立子查询(子查询可以独立运行)
例:
select
最高分=(select MAX(FMath) from MyStudents),
最低分=(select MIN(FMath) from MyStudents),
平均分=(select AVG(FMath) from MyStudents)
相关子查询(子查询中引用了父查询中的结果)
例:
select * from Student
where
exists(select *
from Class
where cName='高二二班' and Class.clsId=Student.sClassId)
子查询除了可以应用在select中之中,还可以应用在delete、update中。
例:--删除刘备、关羽、张飞的成绩
delete from Score
where studentId in
(Select sId from Student where sName='刘备' or sName='关羽' or sName='张飞')
在where中 可以应用子查询
例:--查询高二二班的所有学生
select * from Student
where sClassId=(
select clsId
from Class where cName='高二二班')
exists子查询
它返回逻辑值true或false,并不生产其他任何实际值。所以这种子查询的选择列表常用“SELECT *”格式。
例:
if(exists(select * from Student))
begin--{
   print '有结果'
end--}
else
begin--{
   print '没结果'
end--}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值