MySQL数字辅助表
发布日期:2021-08-29 16:14:28 浏览次数:17 分类:技术文章

本文共 1549 字,大约阅读时间需要 5 分钟。

最近在做一个活动签到的功能,每个用户每天签到,累计到一定次数,可以换一些奖品。
  签到表的设计如下
  CREATE TABLE `award_chance_history` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `awardActId` int(11) DEFAULT NULL COMMENT '活动id',
  `vvid` bigint(20) DEFAULT NULL COMMENT '用户id',
  `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '签到时间',
  `reason` varchar(40) DEFAULT NULL COMMENT '事由',
  `AdditionalChance` int(11) DEFAULT '0' COMMENT '积分变动',
  `type` int(11) DEFAULT NULL COMMENT '类型',
  `Chance_bak` int(11) DEFAULT '0',
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  到了 的阶段,问题来了。
  测试需要让我做一批数据,模拟用户签到了20天,30天..以便测试.
  这在 都不是事儿,用Connect By直接可以解决.
  但是 没有这个功能,
  一开始,我是这么做的
  这种场景其实可以用数字辅助表的方式,在MySQL技术内幕( )中有记录
  首先,创建数字辅助表
create table nums(id int not null primary key);
delimiter $$
create procedure pCreateNums(cnt int)
begin
declare s int default 1;
truncate table nums;
while s<=cnt do
insert into nums select s;
set s=s+1;
end while;
end $$
delimiter ;
delimiter $$
create procedure pFastCreateNums(cnt int)
begin
declare s int default 1;
truncate table nums;
insert into nums select s;
while s*2<=cnt do
insert into nums select id+s from nums;
set s=s*2;
end while;
end $$
delimiter ;
  初始化数据
  call pFastCreateNums(10000);
  创建测试数据
  insert into award_chance_history(awardactid,vvid,reason,additionalChance,type,createtime)
  select 12,70021346,'手机签到测试',10,2,date_add('2014-12-14',interval id day) from nums order by id limit 10;
  这样就创建了从 2014-12-15以后的连续10天签到数据.
最新内容请见作者的GitHub页:http://qaseven.github.io/

转载地址:https://blog.csdn.net/weixin_34007291/article/details/90508122 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:利用大数据技术探索“数字公民”创新
下一篇:基于TestNG 与Selenium 的自动化测试设计与实施

发表评论

最新留言

不错!
[***.144.177.141]2024年04月23日 15时26分39秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章