PLSQL Developer 子 查 询

                           子  查  询

子查询 (内查询) 在主查询之前一次执行完成。子查询的结果被主查询(外查询)使用 。子查询类型还分单行子查询和多行子查询,子查询的使用:子查询要包含在括号内和将子查询放在比较条件的右侧

例如:

select sname

from student

where sno>(select sno

from student

where   sname='匡明')

 

这段代码的意思是找出大于匡明的sno的人等的名字

  1. 单行子查询

单行子查询只返回一行,使用单行比较操作符有‘=’,‘>, >=’,‘<=’,‘< <>

例如:查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的CnoSnoDegree.

select sno,cno,degree

from score

where degree>(select max(degree)from score where cno='3-245')

and cno='3-105'

 

 在子查询中使用使用组函数和 HAVING 子句

例如:查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

select avg(degree) as avgdegree

from score

group by cno

having cno='3-105';

 

在子查询中是非法使用子查询

例如:

select sname

from student

where sno=(select sno from student

where class=95033)

这个例子是不可以查询出来的,得出来的结果

2.多行子查询

IN            等于列表中的任意一个

ANY           和子查询返回的某一个值比较

ALL           和子查询返回的所有值比较

例如:使用ANY

查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

select *

from score

where cno='3-105'

and degree >ANY(select degree from score where cno='3-245')

例如:使用ALL

查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

select cno,sno,degree from score where cno='3-105' and degree>all

(select degree from score where cno='3-245') order by degree desc

例如:使用IN

SELECT *

FROM Student

WHERE sno NOT IN (select sno from student WHERE sname LIKE '%')

注意:如果在IN的后面加上null,就会出现空值

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值