数据库教程:详细聊聊sql中exists和not exists用法

目录exists:exists 和in 的区别not exists详细介绍:附案例分析总结之所以要说这个问题,是因为项目中用到了not exists,但两者写的语句只有一点差别,结果一个有问题了,一个

目录
  • exists:
  • exists 和in 的区别
  • not exists详细介绍:
  • 附案例分析
  • 总结

之所以要说这个问题,是因为项目中用到了not exists,但两者写的语句只有一点差别,结果一个有问题了,一个没问题。具体问题下面详细说明,先来看看exists如何应用。

exists:

强调的是是否有返回集,不需知道具体返回的是什么,比如:         

      select  	*        from  	customer       where  	 not exists (  		select  			0  		from  			customer_goods  		where  			customer_id = 1  	)

只要exists引导的子句有结果集返回,这个条件就算成立。这个返回的字段始终是0,改成1,则始终返回的是1,所以exists不

在乎返回的是什么内容,只在乎是否有结果集返回。

exists 和in 的区别

 这二者最大的区别,是使用in只能返回一个字段值     

     select  	*       from  	customer c       where       c.id   	 not in (  		select  			customer_id  		from  			customer_goods  		where  			customer_id = 1  	)

但exists允许返回多个字段。

not in 和not exists 分别为in 和exists的对立面。

exists(sql 返回结果集为真)

not exists(sql 不返回结果集为真)

not exists详细介绍:

表customer:

详细聊聊sql中exists和not exists用法

 表customer_goods:

详细聊聊sql中exists和not exists用法

二者的干系:customer_goods.customer_id = customer.id

(1) 查询:

     select  	*       from  	customer c       where  	not exists (  		select  			*  		from  			customer_goods	cg                  where                           cg.customer_id =1	  	)

结果:

无返回结果

详细聊聊sql中exists和not exists用法

(2)查询: 

    select  	*      from  	customer c      where  	not exists (  		select  			*  		from  			customer_goods	cg                  where                            c.id =1	  	)

结果:

详细聊聊sql中exists和not exists用法

(3)分析:

发现二者差别只是是否not exists字句查询的查询条件是否跟外面查询条件有关,如果not exists子查询只有自己本身的查询条件,这样只要子查询中有数据返回,就证明是false,结果在整体执行就无返回值;一旦跟外面的查询关联上,就能准确查出数据。

而我遇到的问题正是这个。

经过分析,我认为一旦跟外层查询关联上,就会扫描外面查询的表。而没一旦二者不添加关联关系,只会根据not exists返回是否有结果集来判断,这也是为什么一旦子查询有数据,就查不到所有的数据了。

附案例分析

来看看not exists或exists是如何用的吧。

# 学生表  create table `student`(      `s_id` varchar(20),      `s_name` varchar(20) not null default '',      `s_birth` varchar(20) not null default '',      `s_sex` varchar(10) not null default '',      primary key(`s_id`)  );  # 课程表  create table `course`(      `c_id`  varchar(20),      `c_name` varchar(20) not null default '',      `t_id` varchar(20) not null,      primary key(`c_id`)  );  # 教师表  create table `teacher`(      `t_id` varchar(20),      `t_name` varchar(20) not null default '',      primary key(`t_id`)  );  # 成绩表  create table `score`(      `s_id` varchar(20),      `c_id`  varchar(20),      `s_score` int(3),      primary key(`s_id`,`c_id`)  );  # 插入学生表测试数据  insert into student values('01' , '赵雷' , '1990-01-01' , '男');  insert into student values('02' , '钱电' , '1990-12-21' , '男');  insert into student values('03' , '孙风' , '1990-05-20' , '男');  insert into student values('04' , '李云' , '1990-08-06' , '男');  insert into student values('05' , '周梅' , '1991-12-01' , '女');  insert into student values('06' , '吴兰' , '1992-03-01' , '女');  insert into student values('07' , '郑竹' , '1989-07-01' , '女');  insert into student values('08' , '王菊' , '1990-01-20' , '女');  #课程表测试数据  insert into course values('01' , '语文' , '02');  insert into course values('02' , '数学' , '01');  insert into course values('03' , '英语' , '03');  # 教师表测试数据  insert into teacher values('01' , '张三');  insert into teacher values('02' , '李四');  insert into teacher values('03' , '王五');  #成绩表测试数据  insert into score values('01' , '01' , 80);  insert into score values('01' , '02' , 90);  insert into score values('01' , '03' , 99);  insert into score values('02' , '01' , 70);  insert into score values('02' , '02' , 60);  insert into score values('02' , '03' , 80);  insert into score values('03' , '01' , 80);  insert into score values('03' , '02' , 80);  insert into score values('03' , '03' , 80);  insert into score values('04' , '01' , 50);  insert into score values('04' , '02' , 30);  insert into score values('04' , '03' , 20);  insert into score values('05' , '01' , 76);  insert into score values('05' , '02' , 87);  insert into score values('06' , '01' , 31);  insert into score values('06' , '03' , 34);  insert into score values('07' , '02' , 89);  insert into score values('07' , '03' , 98);  

题目是查询和"01"号的同学学习的课程完全相同的其他同学的信息,直接做确实有点麻烦,我们可以先做做这题:查看学了所有课程的同学的信息。

学了所有课程的同学的信息,那不就是这些同学没有一门课程没有学吗。

select * from student st where not exists(select * from course c   where not exists(select * from score sc where sc.c_id = c.c_id   and sc.s_id = st.s_id));  

详细聊聊sql中exists和not exists用法

然后我们再回过来看这题,把所有的课程换成01同学学的课程。

select * from student st where not exists(select * from  ( select s2.c_id as c_id from student s1   inner join score s2 on s1.s_id = s2.s_id where s1.s_id = 01) t   where  not exists (select * from score sc   where sc.c_id = t.c_id and sc.s_id = st.s_id and st.s_id != 01));  

详细聊聊sql中exists和not exists用法

总结

到此这篇关于sql中exists和not exists用法的文章就介绍到这了,更多相关sql exists和not exists用法内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:详细聊聊sql中exists和not exists用法,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/1054855.html

(0)
上一篇 2022年2月23日
下一篇 2022年2月23日

精彩推荐