• 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条件就返回真。

  • 相关阅读:
    [BZOJ5339] [TJOI2018]教科书般的亵渎
    拉格朗日插值法
    [LOJ#2340] [WC2018] 州区划分
    [洛谷4609] [FJOI2016]建筑师
    [CF960G] Bandit Blues
    [BZOJ2115] [WC2011] Xor
    [Codeforces 809E] Surprise me!
    无需AutoCAD,用C#生成DWG文件
    扩展方法的几点总结
    RedirectStandardOutput
  • 原文地址:https://www.cnblogs.com/mryangbo/p/10904197.html
Copyright © 2020-2023  润新知