zoukankan      html  css  js  c++  java
  • mysql 子查询 EXISTS

    子查询又叫嵌套查询

    子查询的select 语句不能使用order by子句,order by不要只能对最终查询结果排序。

    1.带IN的子查询

    select * from emp where dep_id in (select id from dept id); 在子查询中的order by id排序 对最后结果无影响。

    2.带ANY或ALL的子查询2

    select salary from emp where id in(2,5);

    select * from emp where salary <ANY (select salary from emp where id in(2,5));

    <ANY 子查询(匹配任一返回真) :表示条件满足 小于 子查询中任何一个值 就会返回emp的一条记录,相当于筛选出小于子查询最大值的记录。

    select * from emp where salary <ALL (select salary from emp where id in(2,5));

    <ALL 子查询(匹配所有返回真) :表示条件满足 小于 子查询中所有的值 才会返回emp的一条记录,相当于筛选出子查询最小值的记录。

    其他操作符功能类似(=,<>,<,>,<=,>=)。

    以上用于对子查询出的集合值不明确,且子查询的值不多的情况下,不用直接获取最值而交给数据库匹配的方法。

    3.EXISTS

    select * from emp where EXISTS (select id from dept where dept.id=emp.dep_id);

    用法: exists后面一定是子查询语句,不能用(值1,值2)代替;where exists (查询),结构中没有列;exists后面的子查询不返回任何实际数据,只返回真或假,当返回真时 where条件成立,该条记录保留。

    exists (查询),只要子查询不会空  则where条件就返回真。

  • 相关阅读:
    ok~加油!
    解析window.open链接的参数
    Arrya数组添加过滤条件
    Oracle 查询今天、昨日、本周、本月和本季度的所有记录
    Sql Server日期查询-SQL查询今天、昨天、7天内、30天
    Lua 中 pairs 和 ipairs 的区别
    关于SignalR连接数量问题的记录
    IceStorm示例运行步骤
    从 OPC 到 OPC UA
    SQL Server 2008 R2 Express Profiler
  • 原文地址:https://www.cnblogs.com/mryangbo/p/10904197.html
Copyright © 2011-2022 走看看