用redis存储关系型数据,查询出符合条件的数据随机抽取

脑洞大开,用redis存关系型数据,简直神经病。

算了,既然研究了,就写一下吧。


redis命令的中文解释:http://redis.readthedocs.org/en/latest/index.html


试题信息:

mysql中试题信息

Id难度题型年级题干,答案等其他字段。。。
1231。。。
2322。。。
3111。。。
4121。。。






redis中存储:

Hash类型(用于存储题目的内容):

hmset question_1 hard 2 type 3 level 1 content "{Id:1,hard:2,type:3,level:1,....}"

hmset question_2 hard 3 type 2 level 2 content "{Id:2,hard:3,type:2,level:2,....}"

hmset question_3 hard 1 type 1 level 1 content "{Id:3,hard:1,type:1,level:1,....}"

hmset question_4 hard 1 type 2 level 1 content "{Id:4,hard:1,type:2,level:1,....}"


set类型(用语存储每个条件关联的试题):

sadd hard_1 3 4

sadd hard_2 1

sadd hard_3 2


sadd type_1 3

sadd type_2 2 4

sadd type_3 1


sadd level_1 1 3 4

sadd level_2 2


就此,数据源已经有了。

现在要随机抽取两个 一年级的,题型是1或者2的,难度为1的题目

首先取题型1和2的并集,放到集合temp1中

sunionstore temp1 type_1 type_2

查看temp1内容是 2,3,4

然后取temp1和一年级,难度1的交集,放到temp2中

sinterstore temp2 temp1 level_1 hard_1

查看temp2内容是 3,4

随机的取出temp2中的数据

spop temp2

spop temp2

分别取出了4,3

最后删除temp1和temp2

del temp1

del temp2

用3,4取出具体的题目。

hget question_4 content

hget question_3 content


这一种方法是使用store存到临时的集合,并用pop随机的取出题目

也可以不用,直接取出交集后输出给程序,让程序内存中去完成随机取题的操作。

转载于:https://my.oschina.net/NormanZL/blog/370708

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值