Redis中五种数据类型的常用数据操作之Zset类型

ZSet类型

zset是有序set集合, 专门用来做数据排序。

  1. 添加元素:zadd key [NX|XX] [CH] [INCR] score member [score member …]
    127.0.0.1:6379> zadd z1 80 math 70 english 90  chinese
    (integer) 3
    
  2. 查询指定成员的分数:zscore key member
    127.0.0.1:6379> zscore z1 math
    "80"
    
  3. 删除集合中指定的成员:zrem key member [member …]
    127.0.0.1:6379> zrem z1 math english
    (integer) 2
    
  4. 范围查询:zrange key start stop [WITHSCORES]
    127.0.0.1:6379> zrange z1 0 -1 withscores
    1) "english"
    2) "70"
    3) "math"
    4) "80"
    5) "chinese"
    6) "90"
    
  5. 获取倒序结果:zrevrange key start stop [WITHSCORES]
    127.0.0.1:6379> zrevrange z1 0 -1 withscores
    1) "chinese"
    2) "90"
    3) "math"
    4) "80"
    5) "english"
    6) "70"
    
  6. 按分数范围删除元素:zremrangebyscore key min max
    127.0.0.1:6379> zremrangebyscore z1 75 85
    (integer) 1
    
  7. 在指定的分数范围之内进行排序:zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
    127.0.0.1:6379> zrangebyscore z1 65 95 withscores
    1) "english"
    2) "70"
    3) "chinese"
    4) "90"
    
  8. 返回成员在集合中的排名:zrank key member
    127.0.0.1:6379> zrank z1 english
    (integer) 0
    127.0.0.1:6379> zrank z1 math
    (integer) 1
    
  9. 反转排名:zrevrank key member
    127.0.0.1:6379> zrevrank z1 english
    (integer) 2
    127.0.0.1:6379> zrevrank z1 math
    (integer) 1
    
  10. 给指定的成员增加指定的分数,返回增加以后的分数:zincrby key increment member
    127.0.0.1:6379> zincrby z1 4 math
    "84"
    
  11. 获取分数在[min, max]之间的成员的个数:zcount key min max
    127.0.0.1:6379> zcount z1 65 85
    (integer) 2
    
  12. 按排名范围删除元素:zremrangebyrank key start stop
    127.0.0.1:6379> zremrangebyrank z1 0 1
    (integer) 2
    127.0.0.1:6379> zrange z1 0 -1 withscores
    1) "chinese"
    2) "90"
    
  13. 获取集合数据总量:zcard key
    127.0.0.1:6379> zcard  z1
    (integer) 1
    
  14. zinterstore destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MI
    127.0.0.1:6379> zadd z2 85 sport 75 chinese 90 math
    (integer) 3
    127.0.0.1:6379> zadd z1 80 math 90 chinese 60 english
    (integer) 3
    127.0.0.1:6379> zinterstore zz1 2 z1 z2
    (integer) 2
    127.0.0.1:6379> zrange zz1 0 -1
    1) "chinese"
    2) "math"
    
  15. zunionstore destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
    127.0.0.1:6379> zunionstore zz2 2 z1 z2
    (integer) 4
    127.0.0.1:6379> zrange zz2 0 -1
    1) "english"
    2) "sport"
    3) "chinese"
    4) "math"
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何呵呵是大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值