美文网首页
zset类型操作3

zset类型操作3

作者: 闲云野鹤_23dd | 来源:发表于2020-12-16 18:56 被阅读0次

    ZCOUNT

    语法

    ZCOUNT key min max
    返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。

    返回值 score 值在 min 和 max 之间的成员的数量。

    例子
    redis› ZRANGE salary 0 -1 WITHSCORES # 测试数据1)
    "jack"2) "2000"3) "peter"4) "3500"5) "tom"6) "5000"redis
    › ZCOUNT salary 2000 5000 # 计算薪水在 2000-5000 之间的人数
    (integer) 3
    redis› ZCOUNT salary 3000 5000 # 计算薪水在 3000-5000 之间的人数
    (integer) 2
    ZRANGEBYSCORE

    语法

    ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
    返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。有序集成员按 score 值递增(从小到大)次序排列。

    具有相同 score 值的成员按字典序(lexicographical order)来排列(该属性是有序集提供的,不需要额外的计算)。 可选的 LIMIT 参数指定返回结果的数量及区间(就像SQL中的 SELECT LIMIT offset, count ) 可选的 WITHSCORES 参数决定结果集是单单返回有序集的成员,还是将有序集成员及其 score 值一起返回。

    区间及无限

    min 和 max 可以是 -inf 和 +inf ,这样一来,你就可以在不知道有序集的最低和最高 score 值的情况下,使用 ZRANGEBYSCORE 这类命令。
    默认情况下,区间的取值使用闭区间 (小于等于或大于等于),你也可以通过给参数前增加 ( 符号来使用可选的开区间 (小于或大于)。

    举个例子:

    ZRANGEBYSCORE zset (1 5
    返回所有符合条件 1 ‹ score ‹= 5 的成员,而

    ZRANGEBYSCORE zset (5 (10
    则返回所有符合条件 5 ‹ score ‹ 10 的成员。

    例子
    '''redis › ZADD salary 10086 jack(integer) 1
    redis › ZADD salary 5000 tom(integer) 1
    redis › ZADD salary 7500 peter(integer) 1
    redis › ZADD salary 3500 joe(integer) 1
    redis › ZREVRANGEBYSCORE salary +inf -inf # 逆序排列所有成员1) "jack"2) "peter"3) "tom"4) "joe"
    redis › ZREVRANGEBYSCORE salary 10000 2000 # 逆序排列薪水介于 10000 和 2000 之间的成员1) "peter"2) "tom"3) "joe"
    redis › ZREVRANGEBYSCORE salary +inf -inf LIMIT 1 1 # 工资排名第二的成员"peter"
    ZREVRANGEBYSCORE
    '''
    语法

    ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
    返回有序集 key 中, score 值介于 max 和 min 之间(默认包括等于 max 或 min )的所有的成员。有序集成员按 score 值递减(从大到小)的次序排列。

    具有相同 score 值的成员按字典序的逆序(reverse lexicographical order )排列。

    除了成员按 score 值递减的次序排列这一点外, ZREVRANGEBYSCORE 命令的其他方面和 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] 命令一样。

    返回值: 指定区间内,带有 score 值(可选)的有序集成员的列表。

    例子
    redis › ZADD salary 10086 jack(integer) 1
    redis › ZADD salary 5000 tom(integer) 1
    redis › ZADD salary 7500 peter(integer) 1
    redis › ZADD salary 3500 joe(integer) 1
    redis › ZREVRANGEBYSCORE salary +inf -inf # 逆序排列所有成员1) "jack"2) "peter"3) "tom"4) "joe"
    redis › ZREVRANGEBYSCORE salary 10000 2000 # 逆序排列薪水介于 10000 和 2000 之间的成员1) "peter"2) "tom"3) "joe"

    相关文章

      网友评论

          本文标题:zset类型操作3

          本文链接:https://www.haomeiwen.com/subject/roouiktx.html