Redis命令-有序集合-zrangebyscore

 

原文

http://redis.io/commands/zrangebyscore

 

简介

Return a range of members in a sorted set, by score.

 

根据分数,返回有序集合中一定范围内的元素。

 

语法

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

 

版本

Available since 1.0.5.

 

自1.0.5版本可用。

 

时间复杂度

Time complexity: O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

 

O(log(N)+M):N是有序集合中元素的数量,M是返回的元素的数量。如果M是常量,你可以认为时间复杂度为O(log(N))。

 

描述

Returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.

 

返回有序集合中分数在min和max之间的所有元素(包含分数等于min或max的元素)。这些元素被认为是按照分数由低到高排序。

 

The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).

 

对于分数相同的元素按照词典顺序返回。

 

The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.

 

LIMIT参数可以被用于仅仅获取一定范围内匹配的元素(类似于SQL语句SELECT LIMIT offset, count)。请牢记,如果offset比较大,在获取返回的元素之前,有序集合需要穿过offset个元素,时间复杂度需要加上O(N)。

 

The optional WITHSCORES argument makes the command return both the element and its score, instead of the element alone. This option is available since Redis 2.0.

 

参数WITHSCORES使这个命令返回元素和它的分数,而不仅仅返回元素。这个选项自Redis 2.0可用。

 

Exclusive intervals and infinity

min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.

 

min和max可以是-inf和+inf,因此你不需要知道有序集合中最高分数和最低分数也可以获取从一定分数开始或者达到一定分数结束的所有元素。

 

By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (. For example:

 

默认情况下,min和max是包含在内的。可以通过在分数前面添加(来指定一个开区间(即不包含在内)。

 

ZRANGEBYSCORE zset (1 5

 

Will return all elements with 1 < score <= 5 while:

 

将返回分数大于1且小于等于5的所有元素。

 

ZRANGEBYSCORE zset (5 (10

 

Will return all the elements with 5 < score < 10 (5 and 10 excluded).

 

将返回分数大于5且小于10的所有元素。

 

返回值

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

 

Array:指定分数范围内的元素列表。

 

例子

redis>  ZADD myzset 1 "one"
(integer) 1
redis>  ZADD myzset 2 "two"
(integer) 1
redis>  ZADD myzset 3 "three"
(integer) 1
redis>  ZRANGEBYSCORE myzset -inf +inf
1) "one"
2) "two"
3) "three"
redis>  ZRANGEBYSCORE myzset 1 2
1) "one"
2) "two"
redis>  ZRANGEBYSCORE myzset (1 2
1) "two"
redis>  ZRANGEBYSCORE myzset (1 (2
(empty list or set)
redis>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值