在本教程中将学习如何使用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运算符来选择特定范围内的行数据。