Redis Zrangebyscore Command
Redis Zrangebyscore returns the members of a sorted set within the specified score range. The members are ordered by their score values in ascending (from smallest to largest) order.
Members with the same score are ordered lexicographically (this property is provided by the sorted set and does not require additional computation).
By default, the range uses closed intervals (less than or equal to, greater than or equal to). You can also use open intervals (less than, greater than) by prefixing the parameter with the (
symbol.
For example:
ZRANGEBYSCORE zset (1 5
Returns all members with scores satisfying 1 < score <= 5
, while
ZRANGEBYSCORE zset (5 (10
Returns all members with scores satisfying 5 < score < 10
.
Syntax
The basic syntax for the redis Zrangebyscore command is as follows:
redis 127.0.0.1:6379> ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
Available Versions
= 1.0.5
Return Value
A list of sorted set members within the specified range, with optional scores.
Example
redis 127.0.0.1:6379> ZADD salary 2500 jack # Test data
(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 # Display the entire sorted set
1) "jack"
2) "tom"
3) "peter"
redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf WITHSCORES # Display the entire sorted set with member scores
1) "jack"
2) "2500"
3) "tom"
4) "5000"
5) "peter"
6) "12000"
redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf 5000 WITHSCORES # Display all members with salary <= 5000
1) "jack"
2) "2500"
3) "tom"
4) "5000"
redis 127.0.0.1:6379> ZRANGEBYSCORE salary (5000 400000 # Display members with salary greater than 5000 and less than or equal to 400000
1) "peter"