在Set的基础上,增加了一个值,set k1 v1 zset score1 v1

127.0.0.1:6379> zadd myset 1 one # 添加一个值
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three # 添加多个值
(integer) 2
127.0.0.1:6379> zrange myset 0 -1
1) "one"
2) "two"
3) "three"

排序的实现

127.0.0.1:6379> zadd salary 2500 xiaohong # 添加三个用户
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 500 luoyufeng
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf # 显示全部的用户 从小到大
1) "luoyufeng"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZREVRANGE salary 0 -1 # 从大到小排序
1) "zhangsan"
2) "luoyufeng"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores # 显示全部的用户,并且附带工资
1) "luoyufeng"
2) "500"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2500 withscores # 显示工资小于2500员工的升序排列
1) "luoyufeng"
2) "500"
3) "xiaohong"
4) "2500"

移除元素

127.0.0.1:6379> zrange salary 0 -1
1) "luoyufeng"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> zrem salary xiaohong # 移除有序集合中的指定元素
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "luoyufeng"
2) "zhangsan"

获取有序集合中的个数

127.0.0.1:6379> zcard salary # 获取集合中的个数
(integer) 2

获取指定区间的个数

127.0.0.1:6379> zadd myset 1 hello
(integer) 1
127.0.0.1:6379> zadd myset 2 world 3 luoyufeng
(integer) 2
127.0.0.1:6379> zcount myset 1 3 # 获取指定区间的指定数量
(integer) 3
127.0.0.1:6379> zcount myset 1 2
(integer) 2
最后修改:2021 年 03 月 07 日 12 : 42 PM