本文将简单介绍in从句和exists从句在嵌套查询中的用法
当我们需要使用嵌套查询来完成更复杂的检索时
可以在where从句内添加in或exists从句
一、in
1、单个属性待筛选
在这种情况下,in从句的格式为
select xxx
from table_name
where column_name in
(select xxx
from xxx);
下面用一个具体的例子来说明:
在section(排课)表中,我们想找出 在2009年秋季和2010年春季都有开课的课程id
select distinct course_id
from section
where semeter='Fall' and year=2009 and
course_id in
(select course_id
from section
where semeter='Spring' and year=2010);
同理可得
如果我们想找出 在2009年秋季开课 但不在2010年春季开课的课程id
只需要在in之前添加 not 即可
执行顺序的说明:
在上述查询中
数据库系统将会先执行in从句内的查询
也就是说 先返回一张临时表(表内是