sqlserver exists,not exists的用法

exists,not exists的使用方法示例,需要的朋友可以參考下。

學生表:create table student<BR>(<BR> id number(8) primary key,<BR> name varchar2(10),deptment number(8)<BR>)
選課表:create table select_course<BR>(<BR>  ID         NUMBER(8) primary key,<BR>  STUDENT_ID NUMBER(8) foreign key (COURSE_ID) references course(ID),<BR>  COURSE_ID  NUMBER(8) foreign key (STUDENT_ID) references student(ID)<BR>)
課程表:create table COURSE<BR>(<BR>  ID     NUMBER(8) not null,<BR>  C_NAME VARCHAR2(20),<BR>  C_NO   VARCHAR2(10)<BR>)
student表的數據:<BR>        ID NAME            DEPTMENT_ID<BR>---------- --------------- -----------<BR>         1 echo                   1000<BR>         2 spring                 2000<BR>         3 smith                  1000<BR>         4 liter                  2000
course表的數據:<BR>        ID C_NAME               C_NO<BR>---------- -------------------- --------<BR>         1 數據庫               data1<BR>         2 數學                 month1<BR>         3 英語                 english1
select_course表的數據:<BR>        ID STUDENT_ID  COURSE_ID<BR>---------- ---------- ----------<BR>         1          1          1<BR>         2          1          2<BR>         3          1          3<BR>         4          2          1<BR>         5          2          2<BR>         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 <BR>	 (select * from course c where <BR>  		(select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id));        
        ID NAME            DEPTMENT_ID<BR>---------- --------------- -----------<BR>         1 echo                   1000
2.查詢沒有選擇所有課程的學生,即沒有全選的學生。(存在這樣的一個學生,他至少有一門課沒有選),
分析:只要有一個門沒有選,即select * from select_course sc where student_id=t_student.id and course_id<BR>=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 
	(select * from course where 
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME<BR>---------- ---------------<BR>         2 spring<BR>         3 smith<BR>         4 liter
3.查詢一門課也沒有選的學生。(不存這樣的一個學生,他至少選修一門課程),
分析:如果他選修了一門select * from course結果集不為空,not exists 判斷結果為false;
select id,name from t_student 不執行查詢。
SQL> select id,name from t_student where 
	(select * from course where 
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME<BR>---------- ---------------<BR>         4 liter
4.查詢至少選修了一門課程的學生。
SQL> select id,name from t_student where
	(select * from course where  
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME<BR>---------- ---------------<BR>         1 echo<BR>         2 spring<BR>         3 smith
? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享