Latest web development tutorials

Redis Zrange command

Redis ordered set (sorted set)

Redis Zrange return an ordered set, designated members interval.

The position where the members by fractional increments (small to large) to sort.

Members have the same point value lexicographical ordering (lexicographical order) to arrange.

If you need to press members

Decrement (descending) to arrange, please use ZREVRANGE command.

Subscript parameters start and stop are at zero at the end, that is, to the first member of an ordered set represents 0 to 1 indicates that the second member of an ordered set, and so on.

You can also use a negative index to -1 is the last member of -2 is the penultimate member, and so on.

grammar

redis Zrange basic command syntax is as follows:

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

Available versions

> = 1.2.0

return value

Within a specified range, the list of members of the ordered sets with fractional value (optional) in.

Examples

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 ordered set (sorted set)