English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Zrangebyscore command

Redis 有序集合(sorted set)

Redis Zrangebyscore returns a list of members within the specified score range in the sorted set. The members of the sorted set are arranged in ascending order of score value (from small to large).

Members with the same score value are sorted in dictionary order (this property is provided by the ordered set and does not require additional calculation).

By default, the range values of the interval use closed intervals (less than or equal to or greater than or equal), and you can also use optional open intervals (less than or greater) by adding the ( symbol before the parameter.

Here is an example:

ZRANGEBYSCORE zset (1 5

Return all matches 1 < score <= 5 的成员,而

ZRANGEBYSCORE zset (5 (10

则返回所有符合条件 5 < score < 10 的成员。

语法

redis Zrangebyscore 命令基本语法如下:

redis 127.0.0.1:6379> ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

可用版本

>= 1.0.5

返回值

指定区间内,带有分数值(可选)的有序集成员的列表。

在线示例

redis 127.0.0.1:6379> ZADD salary 2500 jack                        # 测试数据
(integer) 0
redis 127.0.0.1:6379> ZADD salary 5000 tom
(integer) 0
redis 127.0.0.1:6379> ZADD salary 12000 peter
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf               # 显示整个有序集
1) "jack"
2) "tom"
3) "peter"
redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf WITHSCORES    # 显示整个有序集及成员的 score 值
1) "jack"
2) "2500"
3) "tom"
4) "5000"
5) "peter"
6) "12000"
redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf 5000 WITHSCORES    # 显示工资 <=5000 的所有成员
1) "jack"
2) "2500"
3) "tom"
4) "5000"
redis 127.0.0.1:6379> ZRANGEBYSCORE salary (5000 400000            # 显示工资大于 5000 小于等于 400000 的成员
1) "peter"

Redis 有序集合(sorted set)