Home > Database > Mysql Tutorial > sqlserver exists,not exists的用法

sqlserver exists,not exists的用法

WBOY
Release: 2016-06-07 17:59:33
Original
1551 people have browsed it

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

学生表:create table student<br>(<br> id number(8) primary key,<br> name varchar2(10),deptment number(8)<br>)
Copy after login
选课表: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>)
Copy after login
课程表:create table COURSE<br>(<br>  ID     NUMBER(8) not null,<br>  C_NAME VARCHAR2(20),<br>  C_NO   VARCHAR2(10)<br>)
Copy after login
student表的数据:<br>        ID NAME            DEPTMENT_ID<br>---------- --------------- -----------<br>         1 echo                   1000<br>         2 spring                 2000<br>         3 smith                  1000<br>         4 liter                  2000
Copy after login
course表的数据:<br>        ID C_NAME               C_NO<br>---------- -------------------- --------<br>         1 数据库               data1<br>         2 数学                 month1<br>         3 英语                 english1
Copy after login
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
Copy after login
1.查询选修了所有课程的学生id、name:(即这一个学生没有一门课程他没有选的。)
Copy after login
分析:如果有一门课没有选,则此时(1)select * from select_course sc where sc.student_id=ts.id 
Copy after login
and sc.course_id=c.id存在null,
Copy after login
这说明(2)select * from course c 的查询结果中确实有记录不存在(1查询中),查询结果返回没有选的课程,
Copy after login
此时select * from t_student ts 后的not exists 判断结果为false,不执行查询。
Copy after login
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));        
Copy after login
        ID NAME            DEPTMENT_ID<br>---------- --------------- -----------<br>         1 echo                   1000
Copy after login
2.查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选),
Copy after login
分析:只要有一个门没有选,即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 ),
Copy after login
因此select id,name from t_student 将执行查询(id为子查询中t_student.id )。
Copy after login
SQL> select id,name from t_student where 
Copy after login
Copy after login
	(select * from course where 
Copy after login
Copy after login
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
Copy after login
Copy after login
Copy after login
        ID NAME<br>---------- ---------------<br>         2 spring<br>         3 smith<br>         4 liter
Copy after login
3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程),
Copy after login
分析:如果他选修了一门select * from course结果集不为空,not exists 判断结果为false;
Copy after login
select id,name from t_student 不执行查询。
Copy after login
SQL> select id,name from t_student where 
Copy after login
Copy after login
	(select * from course where 
Copy after login
Copy after login
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
Copy after login
Copy after login
Copy after login
        ID NAME<br>---------- ---------------<br>         4 liter
Copy after login
4.查询至少选修了一门课程的学生。
SQL> select id,name from t_student where
Copy after login
	(select * from course where  
Copy after login
		(select * from select_course sc where student_id=t_student.id and course_id=course.id));
Copy after login
Copy after login
Copy after login
        ID NAME<br>---------- ---------------<br>         1 echo<br>         2 spring<br>         3 smith
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template