Oracle 专题
专题目录
您的位置:database > Oracle专题 > Oracle Any/Some运算符
Oracle Any/Some运算符
作者:--    发布时间:2019-11-20

在本教程中,您将学习如何使用oracle any运算符将值与列表或子查询进行比较。

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中,someany的行为完全相同,因此它们完全可以互换。

oracle any运算符的例子

如果子查询返回行或列表具有值,则以下语句适用于any运算符:

1. col = any ( list )

如果col在列表中匹配一个或多个值,则表达式的计算结果为true,例如:

select
    product_name,
    list_price
from
    products
where
    list_price = any(
        2200,
        2259.99,
        2269.99
    )
    and category_id = 1;

执行上面示例代码,得到以下结果 -

2. col != any(list)

如果列与列表中的一个或多个值不匹配,则表达式的计算结果为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;

执行上面示例代码,得到以下结果 -

3. col > any (list)

如果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;

执行上面示例代码,得到以下结果 -

4. col >= any (list)

如果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;

执行上面示例代码,得到以下结果 -

5. col < any (list)

如果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;

执行上面示例代码,得到以下结果 -

6. col <= any (list)

如果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运算符将值与列表或子查询进行比较。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册