w3resource

Redis Sorted Sets: ZRANGEBYSCORE

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]

Redis ZRANGEBYSCORE command is used to return all the elements in the sorted set at the key with a score between minimum and maximum. The elements are considered to be ordered from low to high scores. The elements having the same score are returned in lexicographical order.

Syntax:

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Available since

1.0.5.

Return Value

Array reply, the list of elements in the specified score range (optionally with their scores).

Return Value Type

Array

Example: Redis ZRANGEBYSCORE: specify the range to score

127.0.0.1:6379> ZADD mysales 1556 Samsung 2000 Nokis 1800 Micromax
(integer) 3
127.0.0.1:6379> ZADD mysales 2200 Sunsui 1800 MicroSoft 2500 LG
(integer) 3
127.0.0.1:6379> ZRANGEBYSCORE mysales -inf +inf WITHSCORES
 1) "Samsung"
 2) "1556"
 3) "MicroSoft"
 4) "1800"
 5) "Micromax"
 6) "1800"
 7) "Nokis"
 8) "2000"
 9) "Sunsui"
10) "2200"
11) "LG"
12) "2500"
127.0.0.1:6379> ZRANGEBYSCORE mysales 1500 1800 WITHSCORES
1) "Samsung"
2) "1556"
3) "MicroSoft"
4) "1800"
5) "Micromax"
6) "1800"
127.0.0.1:6379> ZRANGEBYSCORE mysales 2000 +inf WITHSCORES
1) "Nokis"
2) "2000"
3) "Sunsui"
4) "2200"
5) "LG"
6) "2500"

Example: Redis ZRANGEBYSCORE: Using ( to the min, max To avoid include

127.0.0.1:6379> ZRANGEBYSCORE mysales (1500 (1800 WITHSCORES
1) "Samsung"
2) "1556"
127.0.0.1:6379> ZRANGEBYSCORE mysales (1800 2200 WITHSCORES
1) "Nokis"
2) "2000"
3) "Sunsui"
4) "2200"

Example: Redis ZRANGEBYSCORE: Using limit offset count

127.0.0.1:6379> ZRANGEBYSCORE mysales -inf +inf WITHSCORES LIMIT 0 3
1) "Samsung"
2) "1556"
3) "MicroSoft"
4) "1800"
5) "Micromax"
6) "1800"
127.0.0.1:6379> ZRANGEBYSCORE mysales -inf +inf WITHSCORES LIMIT 4 10
1) "Sunsui"
2) "2200"
3) "LG"
4) "2500"

Previous: ZRANGEBYLEX
Next: ZRANK



Follow us on Facebook and Twitter for latest update.