zrange
ZRANGE
Apply for Redis Developer Course
|
Redis Regular Inspection / Technical Support
|
Redis Enterprise Server
|
|---|
Query the member list (from lowest score)
Usage: zrange key start stop.
start stop are indexes, the same concept used by lrange.
Members are returned from lowest score. If scores are equal, members are compared.
Use 0 -1 to query all members.
Example
| Command> | zadd myzip 2 "Seoul" 3 "Busan" 5 "Daegu" 4 "Incheon" 6 "Gwangju" 1 "Gyeonggi" |
| Result> | 6 |
| Command> | zrange myzip 0 -1 Sorted by score |
| Result> |
0) Gyeonggi 1) Seoul 2) Busan 3) Incheon 4) Daegu 5) Gwangju |
| Command> | zrange myzip 0 -1 withscores Use withscores to display scores |
| Result> |
0) 1 -> Gyeonggi 1) 2 -> Seoul 2) 3 -> Busan 3) 4 -> Incheon 4) 5 -> Daegu 5) 6 -> Gwangju |
Query a subset
Give a range with index (start, stop).
Example
| Command> | zrange myzip 1 3 withscores Query from the 2nd to the 4th member |
| Result> |
1) 2 -> Seoul 2) 3 -> Busan 3) 4 -> Incheon |
| Command> | zrange myzip 3 -1 withscores Query from the 4th member to the end |
| Result> |
3) 4 -> Incheon 4) 5 -> Daegu 5) 6 -> Gwangju |
Negative indexes are allowed
With a negative index, the last member is -1, the previous one is -2, and so on.
Returned when start <= stop. Using -1 for stop is an exception.
Example
| Command> | zrange myzip -3 -1 withscores |
| Result> |
-3) 4 -> Incheon -2) 5 -> Daegu -1) 6 -> Gwangju |
If scores are equal, members are sorted lexicographically
Example
| Command> | del myzip |
| Result> | 1 |
| Command> | zadd myzip 10 "Chungcheong" 10 "Jeolla" 10 "Seoul" 10 "Gyeonggi" 10 "Busan" |
| Result> | 5 |
| Command> | zrange myzip 0 -1 withscores |
| Result> |
0) 10 -> Gyeonggi 1) 10 -> Busan 2) 10 -> Seoul 3) 10 -> Jeolla 4) 10 -> Chungcheong |
Use REV to query in reverse order
Use the REV option to query in reverse order.
This feature is available since version 6.2.
This option works like ZREVRANGE.
This option cannot be used together with BYSCORE or BYLEX described below.
Example
| Command> | zadd mycity 10 Seoul 20 Busan 30 Daegu 40 Incheon 50 Gwangju 60 Gyeonggi |
| Result> | 6 |
| Command> | zrange mycity 0 -1 |
| Result> |
1) "Seoul" 2) "Busan" 3) "Daegu" 4) "Incheon" 5) "Gwangju" 6) "Gyeonggi" |
| Command> | zrange mycity 0 -1 rev |
| Result> |
1) "Gyeonggi" 2) "Gwangju" 3) "Incheon" 4) "Daegu" 5) "Busan" 6) "Seoul" |
Use BYSCORE to filter by score
Use the BYSCORE option to filter by score.
You can use '(' for exclusive bounds.
This feature is available since version 6.2.
This option works like ZRANGEBYSCORE.
Example
| Command> | zrange mycity 10 50 byscore |
| Result> |
1) "Seoul" 2) "Busan" 3) "Daegu" 4) "Incheon" 5) "Gwangju" |
| Command> | zrange mycity (10 (50 byscore |
| Result> |
1) "Busan" 2) "Daegu" 3) "Incheon" |
Use BYLEX to filter by member
Use the BYLEX option to filter by member.
You must prefix the bound with inclusive '[' or exclusive '('.
Use - + to query all members.
All scores must be equal to get the intended result.
This feature is available since version 6.2.
This option works like ZRANGEBYLEX.
Example
| Command> | zadd mycity2 0 Seoul 0 Busan 0 Daegu 0 Incheon 0 Gwangju 60 Gyeonggi |
| Result> | 6 |
| Command> | zrange mycity2 [Busan [Incheon bylex |
| Result> |
1) "Busan" 2) "Daegu" 3) "Gwangju" 4) "Gyeonggi" 5) "Incheon" |
| Command> | zrange mycity2 (Busan (Incheon bylex |
| Result> |
1) "Daegu" 2) "Gwangju" 3) "Gyeonggi" |
Use LIMIT to restrict the number of results
Use LIMIT offset count to restrict the number of results.
This option can be used only with BYSCORE or BYLEX.
This feature is available since version 6.2.
Example
| Command> | zrange mycity 10 50 byscore limit 0 3 |
| Result> |
1) "Seoul" 2) "Busan" 3) "Daegu" |
| Command> | zrange mycity2 [Busan [Incheon bylex limit 1 3 |
| Result> |
1) "Daegu" 2) "Gwangju" 3) "Gyeonggi" |
Syntax
ZRANGE key start stop [REV] [[BYSCORE|BYLEX] [LIMIT offset count]] [withscores]
- Available since version 1.2.0.
- The REV, BYSCORE|BYLEX, and LIMIT offset count options are available since version 6.2.
- Time complexity is O(log(N)+M). N is the number of members in the set, and M is the number of members returned.
| SQL Select | If you have Redis Enterprise 7.3.0 or later, try SELECT. |
| Related commands | ZREVRANGE, LRANGE, |
| Clients for Java | Jedis, Lettuce, Redisson | Clients for C | Hiredis |
| << ZINCRBY | ZRANGE | ZLS >> |
|---|
