在本教程中,您将学习如何使用oracle any
运算符将值与列表或子查询进行比较。
oracle any
运算符用于将值与子查询返回的值或结果集列表进行比较。下面举例说明any
运算符与列表或子查询一起使用时的语法:
operator any ( v1, v2, v3)
operator any ( subquery)
在这个语法中:
any
运算符前面必须有一个运算符,例如:=
,!=
,>
,>=
,<
,<=
。使用any
运算符将值与列表进行比较时,oracle将初始条件扩展到列表的所有元素,并使用or
运算符将它们组合,如下所示:
select
*
from
table_name
where
c > any (
v1,
v2,
v3
);
oracle将上述查询转换为以下内容:
select
*
from
table_name
where
c > v1
or c > v2
or c > v3;
如果使用any
运算符将一个值与子查询返回的结果集进行比较,则oracle使用exists运算符将查询转换为等效的查询,而不使用any
运算符。 例如,以下语句返回列表价格大于类别编号:1
中产品的任何标价的所有产品:
select
product_name,
list_price
from
products
where
list_price > any(
select
list_price
from
products
where
category_id = 1
)
order by
product_name;
由于查询与any
运算符一起使用子查询,因此oracle执行了单个转换,如下所示:
select
product_name,
list_price
from
products p1
where
exists(
select
list_price
from
products p2
where
category_id = 1
and p1.list_price > p2.list_price
)
order by
product_name;
请注意,如果子查询不返回任何行,则以下条件的计算结果为false
:
operator any (subquery)
因此,整个查询不返回行:
select
*
from
table_name
where
col operator any(subquery);
在oracle中,some
和any
的行为完全相同,因此它们完全可以互换。
如果子查询返回行或列表具有值,则以下语句适用于any
运算符:
如果col
在列表中匹配一个或多个值,则表达式的计算结果为true
,例如:
select
product_name,
list_price
from
products
where
list_price = any(
2200,
2259.99,
2269.99
)
and category_id = 1;
执行上面示例代码,得到以下结果 -
如果列与列表中的一个或多个值不匹配,则表达式的计算结果为true
。参考以下查询语句 -
select
product_name,
list_price
from
products
where
list_price != any(
2200,
2259.99,
2269.99
)
and category_id = 1
order by
list_price desc;
执行上面示例代码,得到以下结果 -
如果col
大于列表中的最小值,则表达式的计算结果为true
。
select
product_name,
list_price
from
products
where
list_price > any(
2200,
2259.99,
2269.99
)
and category_id = 1
order by
list_price desc;
执行上面示例代码,得到以下结果 -
如果col
大于或等于列表中的最小值,则表达式的计算结果为true
。参考以下查询语句 -
select
product_name,
list_price
from
products
where
list_price >= any(
2200,
2259.99,
2269.99
)
and category_id = 1
order by
list_price desc;
执行上面示例代码,得到以下结果 -
如果col
小于列表中的最大值,则表达式的计算结果为true
。
select
product_name,
list_price
from
products
where
list_price < any(
2200,
2259.99,
2269.99
)
and category_id = 1
order by
list_price desc;
执行上面示例代码,得到以下结果 -
如果col
小于或等于列表中的最大值,则表达式的计算结果为true
。
select
product_name,
list_price
from
products
where
list_price <= any(
2200,
2259.99,
2269.99
)
and category_id = 1
order by
list_price desc;
执行上面示例代码,得到以下结果 -
在本教程中,您已学习如何使用oracle any
运算符将值与列表或子查询进行比较。