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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月23日 15时26分39秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity3D 序列帧
2019-04-27
Unity3D 动态创建图集并压入精灵(NGUI)
2019-04-27
C# 将Excel转xml
2019-04-27
Unity3D log写入文件
2019-04-27
Unity3D动态创建摄像机
2019-04-27
unity 根据纪元时间(1970/1/1)转换为DateTime
2019-04-27
C# 字符串md5加密
2019-04-27
unity3D 拖拽旋转3D物体
2019-04-27
unity3D LineRender的使用:插值移动终点
2019-04-27
unity3d 根据手指触摸的位置去放置UI
2019-04-27
加锁单例
2019-04-27
unity3D 让粒子在UI上播放
2019-04-27
C++的dll导出类
2019-04-27
Matlab与C++混合编程
2019-04-27
unity3D 射线球体投射碰撞检测
2019-04-27
Unity3D 摇一摇功能
2019-04-27
unity3D 在屏幕边框创建碰撞框
2019-04-27
unity3D 音频播放
2019-04-27
xml中常用的转义符
2019-04-27
腾讯MSDK手Q微信授权登录
2019-04-27