zoukankan      html  css  js  c++  java
  • 数据库exists

      EXISTS用于检查子查询是否返回结果,该子查询返回结果为:TRUE/FALSE。也就是说,EXISTS会根据子查询结果的行数返回TRUE/FALSE。

    exists对于子查询结果为null的也视为TRUE。前面已经说过EXISTS会根据子查询结果的行数来判断返回TRUE/FALSE。select null 虽然查出来的结果是null但是还是独占行数的,从行数上而言,select null 的结果是true,只是对于exists而言它是这样。

    对于EXISTS而言外查询的内容会匹配EXISTS的子查询的内容。

    有下面两个表:

    FORUM:

     COMMENT:

     可见forum中的数据是一对多于comment中的。

    select * from forum f;
    /*--等同于   两者都是查询出forum表中的所有数据-*/
    select * from forum f where exists(select * from comment );

    再看如下:

    select * from forum f where exists(
    select * from comment c from c.forum_id=f.forum_id
    );
    /*--等同于-*/
    select f.* from forum f where f.forum_id in(
    select c.forum_id from comment c where c.forum_id=f.forum_id
    );

    可见exists在某种程度上是和in是等效的。

    值得强调的是,exists对于子查询的结果不甚在意,看如下sql

    select * from forum f where exists (
    select c.comment_id from comment c where f.author_id=c.responser_id
    );
    /*-等同于-*/
    select * from forum f where f.author_id in (
    select c.reponser_id from comment where c.responser_id=f.author_id
    );

    not exists和exists反着来,情况都一样的,需要注意的是:not exists 会比not in快,原因是not exists的语法通常会查询索引

  • 相关阅读:
    获取一个日期月的所有日期
    elementUI 日期 周一、周二、周三、周四、周五、周六、周日快捷键
    两个日期之间的天数集合
    数组去重并排序
    日历封装
    <转载> VUE项目中CSS管理
    【转载】Vue项目自动转换 px 为 rem,高保真还原设计图
    HTML5新标签
    pChart图表插件使用
    JavaScript ajax请求
  • 原文地址:https://www.cnblogs.com/notably/p/11770252.html
Copyright © 2011-2022 走看看