Now there are 2 tables,
a book information
create table book (
book int,
title char(20)
)engine myisam charset utf8;
insert into book values
(5 , 'PHP Bible '),
(6 , 'ruby combat '),
(7 , 'mysql operation and maintenance ')
(8, 'ruby server programming ');
a label for a book
create table tags (
time int,
book int,
content char(20)
)engine myisam charset utf8;
insert into tags values
(10 , 5 , 'PHP'),
(11 , 5 , 'WEB'),
(12 , 6 , 'WEB'),
(13 , 6 , 'ruby'),
(14 , 7 , 'database'),
(15 , 8 , 'ruby'),
(16 , 8 , 'server');
In a relational database, if we want to query books with both web tags and PHP tags, we need to use two tables to join the query.
select * from tags inner join tagsas t on tags.bookid=t.bookid
where tags.content='PHP' andt.content='WEB';
In the key-value database of redis , we use key-value to store
set book:5:title 'PHP Bible '
set book:6:title 'ruby combat '
set book:7:title 'mysql is difficult to transport '
set book:8:title ‘ruby server’
sadd tag:PHP 5
sadd tag:WEB 5 6
sadd tag:database 7
sadd tag:ruby 6 8
sadd tag:SERVER 8
Check : both PHP and WEB books
Sinter tag:PHP tag:WEB #Check the intersection of sets
Check : Books with PHP or WEB tags
Sunin tag: PHP tag: WEB
Check : Books with ruby, no WEB tags
Sdiff tag:ruby tag:WEB #求差集