I added six data to sorted set. Then I use zinterstore to get expected result. but there is 0 result.
zadd la.41 41 kfc
zadd la.42 42 mdl
zadd la.43 43 apple
zadd lo.77 77 apple
zadd lo.78 78 mdl
zadd lo.79 79 kfc
zinterstore close 6 la.41 la.42 la.43 lo.77 lo.78 lo.79
expected result:
kfc
mdl
apple
actual result:
0
why ? thanks.
It looks like you either you’re misunderstanding
ZADDand creating more sets than you mean to, or you’re using intersection when you should be using union. Let’s look at both:Creating fewer sets
One potential problem is that you’re creating six sets, and instead you might want to create two sets. In set notation, you’re doing this:
When instead you might want to be doing this:
If this is what you’re intending to do, then you’ve misunderstood the
ZADDcommand.ZADDtakes a key, a score, and a member. You can think of the key as the name of the set, the member as the thing you’re adding to the set, and the score as the member’s position in the set.For example, if I run the command
ZADD foo 1 athen I have created a set calledfoowhich has the memberawith a score of1. Ignoring the score for a second, the situation is now this:If I now run
ZADD foo 2 bthen I’ve added the memberbto the existing setfoo:You are using a different key in each of your
ZADDcommands, so you’re creating a new set with each command. If you use the same key, and therefore the same set:You’ll end up creating a single set:
Union versus intersection
The alternative is that you do want to create six sets, but you want the union and not the intersection.
The union of two sets is a set containing all of the elements that were present in either of the original sets:
The intersection of two sets is a set containing all of the elements that were present in both of the original sets:
You’re creating six sets, which have no common elements and then taking the intersection of them:
If you were taking the union instead you would end up with all of the elements in the resulting set:
You can achieve this by using the
ZUNIONSTORERedis command instead ofZINTERSTORE.