暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mysql查询之exists型子查询

227decision 2020-05-20
645

用法

  • exists型子查询后面跟受限的select查询语句

  • exists后的内层查询能查询出数据,则返回TRUE,表示存在;为空则返回FALSE,表示不存在


使用

    ### websites.id 和 access_log.site_id关联
    mysql> select * from websites;
    +----+--------+
    | id | name |
    +----+--------+
    | 1 | google |
    | 2 | taobao |
    | 3 | baidu |
    +----+--------+


    mysql> select * from access_log;
    +----+---------+
    | id | site_id |
    +----+---------+
    | 1 | 1 |
    | 2 | 2 |
    +----+---------+


    ### 查询有访问记录的网站信息
    mysql> select * from websites a where exists (select 1 from access_log b where a.id=b.site_id);


    +----+--------+
    | id | name |
    +----+--------+
    | 1 | google |
    | 2 | taobao |
    +----+--------+
    2 rows in set


    ### 查询没有访问记录的网站信息
    mysql> select * from websites a where not exists (select 1 from access_log b where a.id=b.site_id);


    +----+-------+
    | id | name |
    +----+-------+
    | 3 | baidu |
    +----+-------+
    1 row in set


    最后修改时间:2020-07-01 12:56:04
    文章转载自227decision,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论