zadd stu 18 lily 19 hmm 20 lilei 21 lilei(integer) 3 (由此..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

有序集合

發(fā)布時間:2020-08-08 20:27:03 來源:網(wǎng)絡(luò) 閱讀:271 作者:梁十八 欄目:關(guān)系型數(shù)據(jù)庫

order set 有序集合

zadd key score1 value1 score2 value2 ..

添加元素
redis 127.0.0.1:6379> zadd stu 18 lily 19 hmm 20 lilei 21 lilei
(integer) 3 (由此看,也是唯一性的)
(排序的依據(jù)是score...)

zrem key value1 value2 ..

作用: 刪除集合中的元素

有序集合

zremrangebyscore key min max

作用: 按照socre來刪除元素,刪除score在[min,max]之間的
redis 127.0.0.1:6379> zremrangebyscore stu 4 10
(integer) 2
redis 127.0.0.1:6379> zrange stu 0 -1
1) "f"

有序集合

zremrangebyrank key start end

作用: 按排名刪除元素,刪除名次在[start,end]之間的
redis 127.0.0.1:6379> zremrangebyrank stu 0 1
(integer) 2
redis 127.0.0.1:6379> zrange stu 0 -1
1) "c"
2) "e"
3) "f"
4) "g"

zrank key member

查詢member的排名(升續(xù) 0名開始)

有序集合

zrevrank key memeber

查詢 member的排名(降續(xù) 0名開始)

ZRANGE key start stop [WITHSCORES]

把集合排序后,返回名次[start,stop]的元素(從0開始)
默認(rèn)是升續(xù)排列
Withscores 是把score也打印出來
有序集合
有序集合

zrevrange key start stop

作用:把集合降序排列,取名字[start,stop]之間的元素

zrangebyscore key min max [withscores] limit offset N

作用: 集合(升續(xù))排序后,取score在[min,max]內(nèi)的元素,
并跳過 offset個, 取出N個
redis 127.0.0.1:6379> zadd stu 1 a 3 b 4 c 9 e 12 f 15 g
(integer) 6
redis 127.0.0.1:6379> zrangebyscore stu 3 12 limit 1 2 withscores
1) "c"
2) "4"
3) "e"
4) "9"

有序集合

有序集合

zcard key

返回元素個數(shù)

有序集合

zcount key min max

返回[min,max] 區(qū)間內(nèi)元素的數(shù)量

zinterstore destination numkeys key1 [key2 ...]
[WEIGHTS weight [weight ...]]
[AGGREGATE SUM|MIN|MAX]

求key1,key2的交集,key1,key2的權(quán)重分別是 weight1,weight2
聚合方法用: sum |min|max
聚合的結(jié)果,保存在dest集合內(nèi)

注意: weights ,aggregate如何理解?
答: 如果有交集, 交集元素又有socre,score怎么處理?
Aggregate sum->score相加 , min 求最小score, max 最大score

另: 可以通過weigth設(shè)置不同key的權(quán)重, 交集時,socre * weights

詳見下例
redis 127.0.0.1:6379> zadd z1 2 a 3 b 4 c
(integer) 3
redis 127.0.0.1:6379> zadd z2 2.5 a 1 b 8 d
(integer) 3
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1
1) "b"
2) "a"
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "4"
3) "a"
4) "4.5"
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 aggregate sum
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "4"
3) "a"
4) "4.5"
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 aggregate min
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "1"
3) "a"
4) "2"
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 weights 1 2
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "5"
3) "a"
4) "7"

有序集合

有序集合

(以賣的最少的那個人的數(shù)據(jù)為準(zhǔn))

有序集合

(以賣的最多的那個人的數(shù)據(jù)為準(zhǔn))

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI