Use RDBMS
Give below 2 tables:
Table: BOOK
BOOK_ID | BOOK_NAME | AUTHOR |
---|---|---|
1 | JAVA | JASON |
2 | RUBY | JASON |
3 | PYTHON | JASON |
4 | PHP | JASON |
5 | OBJECTIVE-C | JASON |
6 | SWIFT | JASON |
Table: TAG
TAG_ID | TAG_NAME | BOOK_ID |
---|---|---|
1 | WEB | 1 |
2 | WEB | 2 |
3 | WEB | 3 |
4 | WEB | 4 |
5 | APPLE | 5 |
6 | APPLE | 6 |
7 | PHP | 4 |
If we want to get the books which both have WEB and PHP tag, we need below SQL:
SELECT T1.BOOK_ID FROM TAG T1 INNER JOIN TAG T2
ON T1.BOOK_ID=T2.BOOK_ID AND T1.TAG_NAME='WEB' AND T2.TAG_NAME='PHP';
Use Redis
prepare the data:
set book;;bookid;;1;;title JAVA
set book;;bookid;;2;;title RUBY
set book;;bookid;;3;;title PYTHON
set book;;bookid;;4;;title PHP
set book;;bookid;;5;;title OBJECTIVE-C
set book;;bookid;;6;;title SWIFT
sadd tag;;WEB 1 2 3 4
sadd tag;;APPLE 5 6
sadd tag;;PHP 4
To get the book both have WEB and PHP tag, use below command:
sinter tag;;WEB tag;;PHP
> 4
e.g. for sinter:
key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SINTER key1 key2 key3 = {c}
To get the book either have WEB or PHP tag, use below command:
sunion tag;;WEB tag;;PHP
> 1 2 3 4
e.g. for sunion:
key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SUNION key1 key2 key3 = {a,b,c,d,e}