1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > mysql之any in some all的区别

mysql之any in some all的区别

时间:2022-03-10 08:42:54

相关推荐

mysql之any in some all的区别

转载:/p/a7530b9845bf

子查询就是指在一个select语句中嵌套另一个select语句。

any,in,some,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 s1 from t1 where s1 > any (select s1 from t2);

假设表t1中有一行包含(10),t2包含(21,14,6),则表达式为true;如果t2包含(20,10),或者表t2为空表,则表达式为false。如果表t2包含(null,null,null),则表达式为unkonwn。

all的意思是“对于子查询返回的列中的所有值,如果比较结果为true,则返回true”

例如:

select s1 from t1 where s1 > all(select s1 from t2);

假设表t1中有一行包含(10)。如果表t2包含(-5,0,+5),则表达式为true,因为10比t2中的查出的所有三个值大。如果表t2包含(12,6,null,-100),则表达式为false,因为t2中有一个值12大于10。如果表t2包含(0,null,1),则表达式为unknown。如果t2为空表,则结果为true。

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理解就有错了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。