本文内容为 http://redis.io/commands/zcount 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
ZCOUNT key min max

返回存储在键 key 的有序集合(Sorted set)中,分值(Score)在最小值 min 及最大值 max 之间的元素的个数。

Returns the number of elements in the sorted set at key with a score between min and max.

minmax 参数与命令 ZRANGEBYSCORE 中的对应选项语义相同。

  • 返回指定分值范围中的元素的个数。

    The number of elements in the specified score range.
  • redis> ZADD myzset 1 "one"
    (integer) 1
    redis> ZADD myzset 2 "two"
    (integer) 1
    redis> ZADD myzset 3 "three"
    (integer) 1
    redis> ZCOUNT myzset -inf +inf
    (integer) 3
    redis> ZCOUNT myzset (1 3
    (integer) 2
  • 版本支持

    2.0.0+

    时间复杂度(Time complexity)

    O(log(N))N 为有序集合中元素的个数。

    O(log(N)) with N being the number of elements in the sorted set.
    The command has a complexity of just O(log(N)) because it uses elements ranks (see ZRANK) to get an idea of the range. Because of this there is no need to do a work proportional to the size of the range.