好比在Northwind数据库中有一个查询为
SELECT c.CustomerId,CompanyName FROM Customers c
WHERE EXISTS(
SELECT OrderID FROM Orders o WHERE o.CustomerID=c.CustomerID)
这里面的EXISTS是如何运做呢?子查询返回的是OrderId字段,但是外面的查询要找的是CustomerID和CompanyName字段,这两个字段确定不在OrderID里面啊,这是如何匹配的呢?
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False
EXISTS 指定一个子查询,检测 行 的存在。
语法: EXISTS subquery
参数: subquery 是一个受限的 SELECT 语句 (不容许有 COMPUTE 子句和 INTO 关键字)。
结果类型: Boolean 若是子查询包含行,则返回 TRUE ,不然返回 FLASE 。html
例表A:TableIn | 例表B:TableEx |
![]() |
![]() |
(一). 在子查询中使用 NULL 仍然返回结果集
select * from TableIn where exists(select null)
等同于: select * from TableIn
(二). 比较使用 EXISTS 和 IN 的查询。注意两个查询返回相同的结果。
select * from TableIn where exists(select BID from TableEx where BNAME=TableIn.ANAME)
select * from TableIn where ANAME in(select BNAME from TableEx)
(三). 比较使用 EXISTS 和 = ANY 的查询。注意两个查询返回相同的结果。
select * from TableIn where exists(select BID from TableEx where BNAME=TableIn.ANAME)
select * from TableIn where ANAME=ANY(select BNAME from TableEx)
NOT EXISTS 的做用与 EXISTS 正好相反。若是子查询没有返回行,则知足了 NOT EXISTS 中的 WHERE 子句。
结论:
EXISTS(包括 NOT EXISTS )子句的返回值是一个BOOL值。 EXISTS内部有一个子查询语句(SELECT ... FROM...), 我将其称为EXIST的内查询语句。其内查询语句返回一个结果集。 EXISTS子句根据其内查询语句的结果集空或者非空,返回一个布尔值。
一种通俗的能够理解为:将外查询表的每一行,代入内查询做为检验,若是内查询返回的结果取非空值,则EXISTS子句返回TRUE,这一行行可做为外查询的结果行,不然不能做为结果。
分析器会先看语句的第一个词,当它发现第一个词是SELECT关键字的时候,它会跳到FROM关键字,而后经过FROM关键字找到表名并把表装入内存。接着是找WHERE关键字,若是找不到则返回到SELECT找字段解析,若是找到WHERE,则分析其中的条件,完成后再回到SELECT分析字段。最后造成一张咱们要的虚表。
WHERE关键字后面的是条件表达式。条件表达式计算完成后,会有一个返回值,即非0或0,非0即为真(true),0即为假(false)。同理WHERE后面的条件也有一个返回值,真或假,来肯定接下来执不执行SELECT。
分析器先找到关键字SELECT,而后跳到FROM关键字将STUDENT表导入内存,并经过指针找到第一条记录,接着找到WHERE关键字计算它的条件表达式,若是为真那么把这条记录装到一个虚表当中,指针再指向下一条记录。若是为假那么指针直接指向下一条记录,而不进行其它操做。一直检索完整个表,并把检索出来的虚拟表返回给用户。EXISTS是条件表达式的一部分,它也有一个返回值(true或false)。
在插入记录前,须要检查这条记录是否已经存在,只有当记录不存在时才执行插入操做,能够经过使用 EXISTS 条件句防止插入重复记录。
INSERT INTO TableIn (ANAME,ASEX)
SELECT top 1 '张三', '男' FROM TableIn
WHERE not exists (select * from TableIn where TableIn.AID = 7)
EXISTS与IN的使用效率的问题,一般状况下采用exists要比in效率高,由于IN不走索引,但要看实际状况具体使用:
IN适合于外表大而内表小的状况;EXISTS适合于外表小而内表大的状况。spring
in、not in、exists和not exists的区别:数据库
先谈谈in和exists的区别:
exists:存在,后面通常都是子查询,当子查询返回行数时,exists返回true。
select * from class where exists (select'x"form stu where stu.cid=class.cid)
当in和exists在查询效率上比较时,in查询的效率快于exists的查询效率
exists(xxxxx)后面的子查询被称作相关子查询, 他是不返回列表的值的.
只是返回一个ture或false的结果(这也是为何子查询里是select 'x'的缘由 固然也能够post
select任何东西) 也就是它只在意括号里的数据能不能查找出来,是否存在这样的记录。
其运行方式是先运行主查询一次 再去子查询里查询与其对应的结果 若是存在,返回ture则输url
出,反之返回false则不输出,再根据主查询中的每一行去子查询里去查询.spa
执行顺序以下:
1.首先执行一次外部查询
2.对于外部查询中的每一行分别执行一次子查询,并且每次执行子查询时都会引用外部查询中当3d
前行的值。
3.使用子查询的结果来肯定外部查询的结果集。
若是外部查询返回100行,SQL 就将执行101次查询,一次执行外部查询,而后为外部查询返回指针
的每一行执行一次子查询。orm
in:包含
查询和全部女生年龄相同的男生
select * from stu where sex='男' and age in(select age from stu where sex='女')
in()后面的子查询 是返回结果集的,换句话说执行次序和exists()不同.子查询先产生结果集,
而后主查询再去结果集里去找符合要求的字段列表去.符合要求的输出,反之则不输出.htm
not in和not exists的区别:
not in 只有当子查询中,select 关键字后的字段有not null约束或者有这种暗示时用not in,另外若是主查询中表大,子查询中的表小可是记录多,则应当使用not in,
例如:查询那些班级中没有学生的,
select * from class where cid not in(select distinct cid from stu)
当表中cid存在null值,not in 不对空值进行处理
解决:select * from class
where cid not in
(select distinct cid from stu where cid is not null)
not in的执行顺序是:是在表中一条记录一条记录的查询(查询每条记录)符合要求的就返回结果集,不符合的就继续查询下一条记录,直到把表中的记录查询完。也就是说为了证实找不到,因此只能查询所有记录才能证实。并无用到索引。
not exists:若是主查询表中记录少,子查询表中记录多,并有索引。
例如:查询那些班级中没有学生的,
select * from class2
where not exists
(select * from stu1 where stu1.cid =class2.cid)
not exists的执行顺序是:在表中查询,是根据索引查询的,若是存在就返回true,若是不存在就返回false,不会每条记录都去查询。
之因此要多用not exists,而不用not in,也就是not exists查询的效率远远高与not in查询的效率。
实例:
学生表:create table student
(
id number(8) primary key,
name varchar2(10),deptment number(8)
)
选课表:create table select_course
(
ID NUMBER(8) primary key,
STUDENT_ID NUMBER(8) foreign key (COURSE_ID) references course(ID),
COURSE_ID NUMBER(8) foreign key (STUDENT_ID) references student(ID)
)
课程表:create table COURSE
(
ID NUMBER(8) not null,
C_NAME VARCHAR2(20),
C_NO VARCHAR2(10)
)
student表的数据:
ID NAME DEPTMENT_ID
---------- --------------- -----------
1 echo 1000
2 spring 2000
3 smith 1000
4 liter 2000
course表的数据:
ID C_NAME C_NO
---------- -------------------- --------
1 数据库 data1
2 数学 month1
3 英语 english1
select_course表的数据:
ID STUDENT_ID COURSE_ID
---------- ---------- ----------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 3 2
1.查询选修了全部课程的学生id、name:(即这一个学生没有一门课程他没有选的。)
分析:若是有一门课没有选,则此时(1)select * from select_course sc where sc.student_id=ts.id
and sc.course_id=c.id存在null,
这说明(2)select * from course c 的查询结果中确实有记录不存在(1查询中),查询结果返回没有选的课程,
此时select * from t_student ts 后的not exists 判断结果为false,不执行查询。
SQL> select * from t_student ts where not exists
(select * from course c where not exists
(select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id));
ID NAME DEPTMENT_ID
---------- --------------- -----------
1 echo 1000
2.查询没有选择全部课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选),
分析:只要有一个门没有选,即select * from select_course sc where student_id=t_student.id and course_id
=course.id 有一条为空,即not exists null 为true,此时select * from course有查询结果(id为子查询中的course.id ),
所以select id,name from t_student 将执行查询(id为子查询中t_student.id )。
SQL> select id,name from t_student where exists
(select * from course where not exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME
---------- ---------------
2 spring
3 smith
4 liter
3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程),
分析:若是他选修了一门select * from course结果集不为空,not exists 判断结果为false;
select id,name from t_student 不执行查询。
SQL> select id,name from t_student where not exists
(select * from course where exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME
---------- ---------------
4 liter
4.查询至少选修了一门课程的学生。
SQL> select id,name from t_student where exists
(select * from course where exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME---------- --------------- 1 echo 2 spring 3 smith