w3resource

Redis Sets: SDIFFSTORE

Redis SDIFFSTORE Command

Redis SDIFFSTORE command store the members of the set, resulting from the difference between the first set and all the successive sets, into the specified key. If the specified key already exists, it is overwritten.

Here are some common uses and scenarios for the "SDIFFSTORE" command:

  • Store Set Difference: Calculate the difference between multiple sets and store the result in a new set.
  • Persistent Exclusion Results: Save the result of set difference operations for later use or reference.
  • Efficient Data Processing: Perform and store set subtraction in a single command, optimizing data processing.
  • Data Comparison and Storage: Compare datasets and persist the unique items from the first set for analysis or reporting.
  • Filtered Data Sets: Create new sets containing only the unique elements of the first set, excluding those in other sets.

Syntax:

SDIFFSTORE DESTINATION_KEY KEY1..KEYN 

Available since:

1.0.0.

Return Value:

Integer replies, the number of elements in the resulting set.

Save to obtain a new set of Difference

mycolor1 = {R G B} 

mycolor2 = {G Y} 

SDIFFSTORE dest_key mycolor1 mycolor2 = {R B} 

Return Value Type:

Integer

Example: Redis SDIFFSTORE

127.0.0.1:6379> SADD mycolor1 R G B
(integer) 3
127.0.0.1:6379> SADD mycolor2 G Y
(integer) 2
127.0.0.1:6379> SDIFFSTORE dest_key mycolor1 mycolor2
(integer) 2
127.0.0.1:6379> SMEMBERS dest_key
1) "R"
2) "B"

Save obtain a set of three car set

How is sdiffstore destination_key source_key1 source_key2 source_key3.

mycolor1 = {R G B} 

mycolor2 = {G Y} 

mycolor3 = {B P}

SDIFFSTORE dest_key mycolor1 mycolor2 mycolor3 = {R}

There is no limit to the number of the key.

127.0.0.1:6379> SADD mycolor3 B P
(integer) 2
127.0.0.1:6379> SDIFFSTORE dest_key mycolor1 mycolor2 mycolor3
(integer) 1
127.0.0.1:6379> SMEMBERS dest_key
1) "R"

Previous: Redis Sets SDIFF
Next: Redis Sets SINTER



Follow us on Facebook and Twitter for latest update.