qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          MySQL數字輔助表

          最近在做一個活動簽到的功能,每個用戶每天簽到,累計到一定次數,可以換一些獎品。
            簽到表的設計如下
            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沒有這個功能,
            一開始,我是這么做的
            這種場景其實可以用數字輔助表的方式,在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天簽到數據.

          posted on 2014-12-11 23:43 順其自然EVO 閱讀(1327) 評論(0)  編輯  收藏 所屬分類: 數據庫

          <2014年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 通许县| 广南县| 毕节市| 类乌齐县| 渭源县| 潮州市| 金华市| 克拉玛依市| 天柱县| 南投县| 哈巴河县| 齐齐哈尔市| 福安市| 海城市| 马山县| 芒康县| 江门市| 搜索| 宜君县| 淄博市| 漾濞| 鱼台县| 阿尔山市| 白银市| 资阳市| 临汾市| 衡山县| 贡嘎县| 嘉定区| 从江县| 新晃| 洪洞县| 灌南县| 建瓯市| 邹平县| 靖远县| 永清县| 永济市| 屯门区| 容城县| 宜兰县|