假设我的数据库表结构是这样的
id name college address
1 xxx nnn xn
2 yyy nnm yn
3 zzz nnz zn
Run Code Online (Sandbox Code Playgroud)
如果我想根据sql中的名称获取学生详细信息,请选择*from student where name ='xxx',以便在redis数据库中如何实现
sbe*_*rry 19
与其他NoSQL数据存储区一样,Redis根据您将要执行的操作有不同的要求.
Redis有几个数据结构,根据您的需要可能很有用.例如,鉴于您对a的渴望,select * from student where name = 'xxx'您可以使用Redis hash.
redis 127.0.0.1:6379> hmset xxx id 1 college nnn address xn
OK
redis 127.0.0.1:6379> hgetall xxx
1) "id"
2) "1"
3) "college"
4) "nnn"
5) "address"
6) "xn"
Run Code Online (Sandbox Code Playgroud)
如果您还有其他查询,就像您想要做同样的事情而选择那样,where college = 'nnn'那么您将不得不对数据进行非规范化.非规范化在SQL中通常是一件坏事,但在NoSQL中它很常见.
如果您的主要查询将违反该名称,但您可能需要查询该大学,那么您可能会执行类似添加set哈希之外的操作.
redis 127.0.0.1:6379> sadd college.nnn xxx
(integer) 1
redis 127.0.0.1:6379> smembers college.nnn
1) "xxx"
Run Code Online (Sandbox Code Playgroud)
如果您的数据结构如此,如果您想查找上大学xn的名称的所有信息,您首先要选择set,然后hash根据返回的名称选择每个set.
您的要求通常会推动您使用的设计和结构.