Redis Zinterstore Command

Redis Sorted Set

Redis Zinterstore is used to calculate the intersection of multiple sorted sets and store the result in a new sorted set.

The Zinterstore command is very useful when you need to perform set operations (for example, computing the intersection) on sorted sets.

By default, the score value of a member in the result set is the sum of the score values of that member 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 resulting sorted set.
  • numkeys: The number of sorted sets to intersect.
  • key: The names of the sorted sets participating in the calculation.
  • WEIGHTS weight [weight ...]: Specifies the weight for each sorted set. The default weight is 1.
  • AGGREGATE sum|min|max: Specifies the aggregation method of the intersection result. Default issum(sum).

Available Versions

>= 2.0.0

Return Value

The number of members saved to the target result set.

Examples

# 有序集 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

# 另一个有序集 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

# 显示有序集内所有成员及其分数值
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"

Redis Sorted Set

Other Extensions