SQL中exists关键字的用法

        exists用于检查一个子查询是否至少会返回一行数据(即检测的存在),返回值为truefalse
        语法: exists subquery
       
参数: subquery是一个受限的select语句(不允许有compute子句和into关键字),该语句返回一个结果集。
        结果类型: boolean类型——如果子查询包含行,则返回true,否则返回false,即言:exists根据subquery的结果集是否为空来返回一个布尔值——如果不为空则返回true,否则返回false

        以例子说明其使用方法:

                            table1                                                table2

                     id             class_name                id             name           class_id

                     01                一年级                   01             张三                01

                     02                二年级                   02             李四                02

                     03                三年级                   04             王五                04

        1、在子查询中使用NULL仍然返回结果集:select * from table1 where exists(select null),该条sql语句等同于:select * from table1,其查询结果为:

                                                             id             class_name

                                                             01                一年级

                                                             02                二年级

                                                             03                三年级

        2select t1.id,t1.class_name from table1 t1 where exists (select * from table2 t2 where t2.class_id = t1.id),该条sql语句等同于:select t1.id,t1.class_name from table1 t1 where t1.id in (select t2.class_id from table2 t2 where t2.class_id = t1.id),其查询的结果为:

                                                            id             class_name

                                                            01                一年级

                                                            02                二年级

        其查询过程为:

        select t1.id,t1.class_name from table1 t1 where exists (select * from table2 t2 where t2.class_id = '01')

        ---> select * from table2 t2 where t2.class_id = '01'有数据存在,所以exists返回true

        select t1.id,t1.class_name from table1 t1 where exists (select * from table2 t2 where t2.class_id = '02')

        ---> select * from table2 t2 where t2.class_id = '02'有数据存在,所以exists返回true

        select t1.id,t1.class_name from table1 t1 where exists (select * from table2 t2 where t2.class_id = '03')

        ---> select * from table2 t2 where t2.class_id = '03'没有数据存在,所以exists返回false

        这种执行过程可以通俗的理解为:将外查询表的每一行,代入内查询作为检验,如果内查询返回的结果取非空值,则exists子句返回true,这一行可作为外查询的结果行,否则不能作为结果。
        分析器会先看语句的第一个词,当它发现第一个词是select关键字的时候,它会跳到from关键字,然后通过from关键字找到表名并把表装入内存,接着是找where关键字,如果找不到则返回到select找字段解析,如果找到where,则分析其中的条件,完成后再回到select分析字段,最后形成一张虚表
        where关键字后面的是条件表达式,条件表达式计算完成后,会有一个返回值,即非00,非0即为真(true)0即为假(false)。如果为正则执行select语句,否则不执行select语句。
        分析器先找到关键字select,然后跳到from关键字,将该关键字后面的表导入内存,并通过指针找到第一条记录,接着找到where关键字计算它的条件表达式,如果为真那么把这条记录装到一个虚表当中,指针再指向下一条记录。如果为假那么指针直接指向下一条记录,而不进行其它操作。一直检索完整个表,并把检索出来的虚拟表返回给用户。exists是条件表达式的一部分,它也有一个返回值(truefalse)

        在插入记录前,需要检查这条记录是否已经存在,只有当记录不存在时才执行插入操作,可以通过使用EXISTS条件句防止插入重复记录。
        insert into table1 (id,class_name) values ('03','四年级') where not exists (select * from table1 where table1 = '03')

        existsin的区别:

        1in引导的子句只能返回一个字段,exists子句可以有多个字段;

        2通常情况下采用exists要比in效率高,因为in不走索引,但要但要具体情况具体分析:in适合于外表大而内表小的情况;exists适合于外表小而内表大的情况。

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值