Redis Zinterstore Command
Redis Zinterstore is used to compute the intersection of multiple sorted sets and store the result in a new sorted set.
The Zinterstore command is very useful when set operations on sorted sets (such as computing intersections) are needed.
By default, the score of a member in the result set is the sum of that member's scores across all given sets.
Syntax
The basic syntax of the redis Zinterstore command is as follows:
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE sum|min|max]
destination: the name of the result sorted set.numkeys: the number of sorted sets to compute the intersection of.key: the names of the sorted sets participating in the computation.WEIGHTS weight [weight ...]: specifies the weight of each sorted set. The default weight is 1.AGGREGATE sum|min|max: specifies the aggregation method of the intersection result. The default issum(sum).
Available Version
= 2.0.0
Return Value
The number of members saved to the target result set.
Example
# Ordered set mid_test
redis 127.0.0.1:6379> ZADD mid_test 70 "Li Lei"
(integer) 1
redis 127.0.0.1:6379> ZADD mid_test 70 "Han Meimei"
(integer) 1
redis 127.0.0.1:6379> ZADD mid_test 99.5 "Tom"
(integer) 1
# another ordered set fin_test
redis 127.0.0.1:6379> ZADD fin_test 88 "Li Lei"
(integer) 1
redis 127.0.0.1:6379> ZADD fin_test 75 "Han Meimei"
(integer) 1
redis 127.0.0.1:6379> ZADD fin_test 99.5 "Tom"
(integer) 1
# 交集
redis 127.0.0.1:6379> ZINTERSTORE sum_point 2 mid_test fin_test
(integer) 3
# Display all members and their scores in the ordered set
redis 127.0.0.1:6379> ZRANGE sum_point 0 -1 WITHSCORES
1) "Han Meimei"
2) "145"
3) "Li Lei"
4) "158"
5) "Tom"
6) "199"
