最近在做一個活動簽到的功能,每個用戶每天簽到,累計到一定次數,可以換一些獎品。
簽到表的設計如下
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天..以便測試.
這在
Oracle都不是事兒,用Connect By直接可以解決.
一開始,我是這么做的
這種場景其實可以用數字輔助表的方式,在MySQL技術內幕(
SQL)中有記錄
首先,創建數字輔助表
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天簽到數據.
English » | | | | | | | | |
Text-to-speech function is limited to 100 characters