在本教程中将学习如何使用oracle between
运算符来选择值在一个范围内的行数据。
between
运算符允许指定要测试的范围。当使用between
运算符为select语句返回的行形成搜索条件时,只返回其值在指定范围内的行。
以下说明between
运算符的语法:
expression [ not ] between low and high
在上面的语法中,
low
和hight
指定要测试的范围的下限值和上限值。low
和hight
值可以是文字或表达式。low
和hight
定义的范围内测试的表达式。 为了能够比较,expression
,low
和hight
的数据类型必须是相同的。and
运算符充当占位符来分隔low
和hight
的值。如果表达式(expression
)的值大于或等于low
的值,小于或等于hight
的值,则between
运算符返回true
。
value >= low and value <= high
not between
运算符否定between
运算符的结果。
下面来看看使用oracle between
运算符的一些示例。
请参阅示例数据库中的以下products
表:
以下语句返回标准成本在500
到600
之间的所有产品:
select
product_name,
standard_cost
from
products
where
standard_cost between 500 and 600
order by
standard_cost;
在此示例中,我们将标准成本(standard_cost
)列中的值与500
(含)到600
(含)之间的范围进行比较。该查询仅返回标准成本在以下范围之间的产品:
要查询标准成本不在500
和600
之间的产品,请按如下方式将not
运算符添加到上述查询中:
select
product_name,
standard_cost
from
products
where
standard_cost not between 500 and 600
order by
product_name;
执行上面查询语句,得到以下结果 -
我们使用示例数据库中的orders
表进行演示:
以下查询语句将返回2016年12月1日至2016年12月31日期间客户的订单:
select
order_id, customer_id, status, order_date
from
orders
where
order_date between date '2016-12-01' and date '2016-12-31'
order by
order_date;
执行上面查询语句,得到以下结果 -
在本教程中,您已学习如何使用oracle between
运算符来选择特定范围内的行数据。