> 文档中心 > MySQL 数据库中 in、some、any、all 的区别与使用

MySQL 数据库中 in、some、any、all 的区别与使用


MySQL 数据库中 in、some、any、all 的区别与使用

in、some、any、all的理解

  • in:在某个范围内存在,就返回true;

    • in (a,b,c)可以理解成 a or b or c
    • in 的作用与=any的作用相同
  • all:所有,必须与子查询返回的结果一致,才返回true

    • 用法:all(select column from table where 【条件】)
    • all中子查询的结果可以理解为用and将子查询连接
    • 果子查询的结果是空表,则结果都为true
    • 如果子查询的结果中有null值,那么最终结果都是null
  • any:任一,与子查询返回的结果任何一个相同,结果就返回true

    • 用法:any(select column from table where 【条件】)
    • 子查询的结果可以理解为用 or 连接起来
    • 如果子查询的结果是空表或者有空值的情况,那么结果都是null
  • some:一些,是any的别名,不常用

    in、any、all的使用

    #in 的用法select * from user where id in (1,2,3,4);#any 的用法select * from user where id any (1,2,3,4);#错误用法,any 和 all要结合=、>、>=、<、<=、使用select * from user where id = any (select id from user where id in (1,2,3,4));#all 的用法select * from user where id > all (select id from user where id in (1,2,3,4));