这篇文章主要介绍了sql中exists和notexists怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

exists:

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

SELECT*FROMcustomerWHEREnotEXISTS(SELECT0FROMcustomer_goodsWHEREcustomer_id=1)

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

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

exists 和in 的区别

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

SELECT*FROMcustomercWHEREc.idnotin(SELECTcustomer_idFROMcustomer_goodsWHEREcustomer_id=1)

但exists允许返回多个字段。

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

exists(sql 返回结果集为真)

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

not exists详细介绍:

表customer:

表customer_goods:

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

(1)查询:

SELECT*FROMcustomercWHERENOTEXISTS(SELECT*FROMcustomer_goodscgWHEREcg.customer_id=1)

结果:

无返回结果

(2)查询:

SELECT*FROMcustomercWHERENOTEXISTS(SELECT*FROMcustomer_goodscgWHEREc.id=1)

结果:

(3)分析:

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

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

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

附案例分析

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

#学生表CREATETABLE`Student`(`s_id`VARCHAR(20),`s_name`VARCHAR(20)NOTNULLDEFAULT'',`s_birth`VARCHAR(20)NOTNULLDEFAULT'',`s_sex`VARCHAR(10)NOTNULLDEFAULT'',PRIMARYKEY(`s_id`));#课程表CREATETABLE`Course`(`c_id`VARCHAR(20),`c_name`VARCHAR(20)NOTNULLDEFAULT'',`t_id`VARCHAR(20)NOTNULL,PRIMARYKEY(`c_id`));#教师表CREATETABLE`Teacher`(`t_id`VARCHAR(20),`t_name`VARCHAR(20)NOTNULLDEFAULT'',PRIMARYKEY(`t_id`));#成绩表CREATETABLE`Score`(`s_id`VARCHAR(20),`c_id`VARCHAR(20),`s_score`INT(3),PRIMARYKEY(`s_id`,`c_id`));#插入学生表测试数据insertintoStudentvalues('01','赵雷','1990-01-01','男');insertintoStudentvalues('02','钱电','1990-12-21','男');insertintoStudentvalues('03','孙风','1990-05-20','男');insertintoStudentvalues('04','李云','1990-08-06','男');insertintoStudentvalues('05','周梅','1991-12-01','女');insertintoStudentvalues('06','吴兰','1992-03-01','女');insertintoStudentvalues('07','郑竹','1989-07-01','女');insertintoStudentvalues('08','王菊','1990-01-20','女');#课程表测试数据insertintoCoursevalues('01','语文','02');insertintoCoursevalues('02','数学','01');insertintoCoursevalues('03','英语','03');#教师表测试数据insertintoTeachervalues('01','张三');insertintoTeachervalues('02','李四');insertintoTeachervalues('03','王五');#成绩表测试数据insertintoScorevalues('01','01',80);insertintoScorevalues('01','02',90);insertintoScorevalues('01','03',99);insertintoScorevalues('02','01',70);insertintoScorevalues('02','02',60);insertintoScorevalues('02','03',80);insertintoScorevalues('03','01',80);insertintoScorevalues('03','02',80);insertintoScorevalues('03','03',80);insertintoScorevalues('04','01',50);insertintoScorevalues('04','02',30);insertintoScorevalues('04','03',20);insertintoScorevalues('05','01',76);insertintoScorevalues('05','02',87);insertintoScorevalues('06','01',31);insertintoScorevalues('06','03',34);insertintoScorevalues('07','02',89);insertintoScorevalues('07','03',98);

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

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

select*fromStudentstwherenotexists(select*fromCoursecwherenotexists(select*fromScorescwheresc.c_id=c.c_idandsc.s_id=st.s_id));

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

select*fromStudentstwherenotexists(select*from(selects2.c_idasc_idfromStudents1innerjoinScores2ons1.s_id=s2.s_idwheres1.s_id=01)twherenotexists(select*fromScorescwheresc.c_id=t.c_idandsc.s_id=st.s_idandst.s_id!=01));

感谢你能够认真阅读完这篇文章,希望小编分享的“sql中exists和notexists怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!