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
-
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));