in、not in、exists和not exists的區別: 1.先談談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'的原因 當然也可以select任何東西) 也就是它只在乎括號里的數據能不能查找出來,是否存在這樣的記錄。 其運行方式是先運行主查詢一次 再去子查詢里查詢與其對應的結果 如果存在,返回ture則輸出,反之返回false則不輸出,再根據主查詢中的每一行去子查詢里去查詢. 執行順序如下: 1.首先執行一次外部查詢 2.對於外部查詢中的每一行分別執行一次子查詢,而且每次執行子查詢時都會引用外部查詢中當前行的值。 3.使用子查詢的結果來確定外部查詢的結果集。 如果外部查詢返回100行,SQL就將執行101次查詢,一次執行外部查詢,然后為外部查詢返回的每一行執行一次子查詢。 2.in:包含 查詢和所有女生年齡相同的男生 select * from stu where sex='男' and age in(select age from stu where sex='女') in()后面的子查詢 是返回結果集的,換句話說執行次序和exists()不一樣.子查詢先產生結果集, 然后主查詢再去結果集里去找符合要求的字段列表去.符合要求的輸出,反之則不輸出. 3.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查詢的效率。 實例: exists,not exists的使用方法示例,需要的朋友可以參考下。 學生表: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