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

Redis Zrange Command

Redis Zrange Command

Redis Sorted Set

Redis Zrange returns the members within the specified range of the sorted set.

The positions of members are sorted by score in ascending order (from small to large).

Members with the same score are arranged in lexicographical order.

If you need members sorted by

value in descending order (from large to small), please use ZREVRANGE command.

The index parameters start and stop are both zero-based; that is, 0 represents the first member of the sorted set, 1 represents the second member, and so on.

You can also use negative indices, with -1 for the last member, -2 for the second-to-last member, and so on.

Syntax

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



redis 127.0.0.1:6379> ZRANGE key start stop [WITHSCORES]

Available Version

= 1.2.0

Return Value

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

Example



redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES             # 显示整个有序集成员

1) "jack"

2) "3500"

3) "tom"

4) "5000"

5) "boss"

6) "10086"



redis 127.0.0.1:6379> ZRANGE salary 1 2 WITHSCORES              # 显示有序集下标区间 1 至 2 的成员

1) "tom"

2) "5000"

3) "boss"

4) "10086"



redis 127.0.0.1:6379> ZRANGE salary 0 200000 WITHSCORES         # 测试 end 下标超出最大下标时的情况

1) "jack"

2) "3500"

3) "tom"

4) "5000"

5) "boss"

6) "10086"



redis > ZRANGE salary 200000 3000000 WITHSCORES                  # 测试当给定区间不存在于有序集时的情况

(empty list or set)

Redis Sorted Set