Manon.icu

I'm here to make you a better developer by teaching you everything I know about building for the web.

Published 2022-06-06

Postgresql - BETWEEN

使用BETWEEN运算符将一个值与一系列值进行匹配

语法

value BETWEEN low AND high

如果valuelowhigh之间,则返回TRUE,否则返回FALSE

BETWEEN可以使用大于等于和小于等于运算符,但是不能使用LIKE运算符。

value >= low and value <= high

检查是否超出范围,可以使用NOTBETWEEN组合运算符。

value NOT BETWEEN low AND high

等价于

value < low OR value > high

示例

使用BETWEEN查询金额是否在 8 和 9 之间。

SELECT
	customer_id,
	payment_id,
	amount
FROM
	payment
WHERE
	amount BETWEEN 8 AND 9;

x42PKI

查询金额不在 8 和 9 之间。

SELECT
	customer_id,
	payment_id,
	amount
FROM
	payment
WHERE
	amount NOT BETWEEN 8 AND 9;

Fx8VVk