作者:李永健
撰写时间:2019年 3月29日
开发工具与关键技术:Oracle sql*plus 、 PLSQL Developer
查询和“s001”号的同学学习的课程完全相同的其他同学学号。
1、首先我们要知道s001同学学习过的课程:
select cno(课程ID) from sc2 where sno = 's001'
2、和查询出它没有学习过的课程,因为要与它课程完全相同,
所有学习与它相同的并且其它的课程都不可以学:
Select cno from sc2
Where cno not in (select cno from sc2 where sno = 's001')
3、查询出s001同学学习了多少门课程
Select count (cno) from sc2 where sno = 's001'
然后通过子查询将它们合并,答案如下:
select sno(学号ID), count(cno) from sc2
Where cno in (select cno from sc2 where sno = 's001') and cno not in (
Select cno from sc2
Where cno not in (select cno from sc2 where sno = 's001')) and sno! = 's001'
Group by sno
Having count (cno) = (select count (cno) from sc2 where sno = 's001')
效果图:
详细说明函数的作用如下:
1、 Count:求行总数
例子:
Select count (*) from employees
这employee表107条数据
2、 in :筛选出右边数据中任意相同都行
例子:
Select department_id from employees where department_id in (80, 90)
3、not in :筛选出不与右边相同的数据
4、group bu :分组函数,对数据进行分组
5、where 与 having :都可以筛选出想要的数据,where中不可以使用分组函数
6、子查询:子查询 (内查询) 在主查询之前一次执行完成,子查询的结果被主查询(外查询)使用