any 可以与=、>、>=、<、<=、<>结合起来使用,分别表示等于、大于、大于等于、小于、小于等于、不等于其中的任意一个数据。
all可以与=、>、>=、<、<=、<>结合是来使用,分别表示等于、大于、大于等于、小于、小于等于、不等于其中的其中的所有数据。
它们进行子查询的语法如下:
operand comparison_operator any (subquery); operand in (subquery); operand coparison_operator some (subquery); operand comparison_operator all (subquery);
any,all关键字必须与一个比较操作符一起使用。
② any关键词可以理解为“对于子查询返回的列中的任一数值,如果比较结果为true,则返回true”。
例如:
select age from t_user where age > any (select age from t_user_copy);
假设表t_user 中有一行包含(10),t_user_copy包含(21,14,6),则表达式为true;如果t_user_copy包含(20,10),或者表t_user_copy为空表,则表达式为false。如果表t_user_copy包含(null,null,null),则表达式为unkonwn。
all的意思是“对于子查询返回的列中的所有值,如果比较结果为true,则返回true”
例如:
select age from t_user where age > all (select age from t_user_copy);
假设表t_user 中有一行包含(10)。如果表t_user_copy包含(-5,0,+5),则表达式为true,因为10比t_user_copy中的查出的所有三个值大。如果表t_user_copy包含(12,6,null,-100),则表达式为false,因为t_user_copy中有一个值12大于10。如果表t_user_copy包含(0,null,1),则表达式为unknown。如果t_user_copy为空表,则结果为true。
③ not in /in
not in 是 “<>all”的别名,用法相同。
语句in 与“=any”是相同的。
例如:
select s1 from t1 where s1 = any (select s1 from t2); select s1 from t1 where s1 in (select s1 from t2);
语句some是any的别名,用法相同。
例如:
select s1 from t1 where s1 <> any (select s1 from t2); select s1 from t1 where s1 <> some (select s1 from t2);
在上述查询中some理解上就容易了“表t1中有部分s1与t2表中的s1不相等”,这种语句用any理解就有错了。
总结
到此这篇关于MySQL中exists、in及any基本用法的文章就介绍到这了,更多相关MySQL exists、in及any内容请搜索
更多相关Mysql内容来自木庄网络博客
标签:Mysql
相关阅读 >>
如何用mysqladministrator备份mysql数据库
更多相关阅读请进入《mysql》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。