sqlserver exists,not exists的用法
更新時間:2009年12月06日 21:32:45 作者:
exists,not exists的使用方法示例,需要的朋友可以參考下。
學(xué)生表: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表的數(shù)據(jù):
ID NAME DEPTMENT_ID
---------- --------------- -----------
1 echo 1000
2 spring 2000
3 smith 1000
4 liter 2000
course表的數(shù)據(jù):
ID C_NAME C_NO
---------- -------------------- --------
1 數(shù)據(jù)庫 data1
2 數(shù)學(xué) month1
3 英語 english1
select_course表的數(shù)據(jù):
ID STUDENT_ID COURSE_ID
---------- ---------- ----------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 3 2
1.查詢選修了所有課程的學(xué)生id、name:(即這一個學(xué)生沒有一門課程他沒有選的。)
分析:如果有一門課沒有選,則此時(1)select * from select_course sc where sc.student_id=ts.id
and sc.course_id=c.id存在null,
這說明(2)select * from course c 的查詢結(jié)果中確實有記錄不存在(1查詢中),查詢結(jié)果返回沒有選的課程,
此時select * from t_student ts 后的not exists 判斷結(jié)果為false,不執(zhí)行查詢。
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.查詢沒有選擇所有課程的學(xué)生,即沒有全選的學(xué)生。(存在這樣的一個學(xué)生,他至少有一門課沒有選),
分析:只要有一個門沒有選,即select * from select_course sc where student_id=t_student.id and course_id
=course.id 有一條為空,即not exists null 為true,此時select * from course有查詢結(jié)果(id為子查詢中的course.id ),
因此select id,name from t_student 將執(zhí)行查詢(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.查詢一門課也沒有選的學(xué)生。(不存這樣的一個學(xué)生,他至少選修一門課程),
分析:如果他選修了一門select * from course結(jié)果集不為空,not exists 判斷結(jié)果為false;
select id,name from t_student 不執(zhí)行查詢。
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.查詢至少選修了一門課程的學(xué)生。
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
相關(guān)文章
SqlServer中查看當(dāng)前Open的Cursor方法
這篇文章主要介紹了SqlServer中查看當(dāng)前Open的Cursor方法,本文為了解決A cursor with thename ' ' already exists總結(jié)的幾個方法,需要的朋友可以參考下2015-07-07sqlserver復(fù)制數(shù)據(jù)庫的方法步驟(圖文)
這篇文章主要介紹了sqlserver復(fù)制數(shù)據(jù)庫的方法步驟(圖文),文中通過圖文示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04如何統(tǒng)計全天各個時間段產(chǎn)品銷量情況(sqlserver)
對一個賣場來說,肯定有負(fù)責(zé)人每天定時去統(tǒng)計每天各個時間段產(chǎn)品銷量情況,以此保證賣場長期可持續(xù)發(fā)展,有的用excel表統(tǒng)計,但是用起來很不方便,下面給大家介紹sqlserver版如何統(tǒng)計全天各個時間段產(chǎn)品銷量情況2015-08-08SQLServer 2000 數(shù)據(jù)庫同步詳細(xì)步驟[兩臺服務(wù)器]
成功實現(xiàn)SQL Server 2000 數(shù)據(jù)庫同步[一臺服務(wù)器,一臺動態(tài)IP的備份機(jī)],詳細(xì)步驟說明。2010-07-07SQLServer用t-sql命令批量刪除數(shù)據(jù)庫中指定表(游標(biāo)循環(huán)刪除)
這篇文章主要介紹了SQLServer用t-sql命令批量刪除數(shù)據(jù)庫中指定表的方法,大家參考使用2013-11-11