Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Zrangebyscore Command

Redis Zrangebyscore Command

Redis Sorted Set

Redis Zrangebyscore returns the list of members in a sorted set within a specified score range. Sorted set members are ordered by ascending score value (from low to high).

Members with the same score are ordered lexicographically (this property is provided by the sorted set and requires no extra computation).

By default, the interval uses closed intervals (less than or equal to, or greater than or equal to). You can also prefix a parameter with ( to use an optional open interval (less than or greater than).

For example:



ZRANGEBYSCORE zset (1 5

returns all members matching condition 1 < score <= 5, while



ZRANGEBYSCORE zset (5 (10

then returns all members matching condition 5 < score < 10 members.

Syntax

The basic syntax of the redis Zrangebyscore command is as follows:



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

Available Version

= 1.0.5

Return Value

A list of sorted set members within the specified range, with score values (optional).

Example



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