码迷,mamicode.com
首页 > 数据库 > 详细

MYSQL 存储过程通用

时间:2018-04-04 18:12:41      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:随机   时间   _for   insert   class   通用   val   col   rand   

返回随机时间 函数

CREATE  FUNCTION `milan_get_rand_time`(type VARCHAR(50)) RETURNS varchar(20) CHARSET utf8
BEGIN
DECLARE _day INT DEFAULT (FLOOR(RAND() * 60)); -- 随机天数 60天以内随机天数
DECLARE _hour INT DEFAULT (FLOOR(RAND() * 24)); -- 随机小时
DECLARE _minute INT DEFAULT (FLOOR(RAND() * 60));-- 随机分
DECLARE _second INT DEFAULT (FLOOR(RAND() * 60));-- 随机秒
DECLARE _myday VARCHAR(20);
IF type = before THEN
        set _day =_day;
ELSEIF type=after THEN
        SET _day =-1*_day;
ELSE
        SET _day=0;
END IF;

 SET _myday = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL _day DAY) ,%Y-%m-%d); -- 获取一个随机的日期
-- SET _myday = DATE_SUB(CURRENT_DATE(),INTERVAL _day DAY); -- 获取一个随机的日期
SET _myday = CONCAT(_myday, ,LPAD(_hour,2,0),:,LPAD(_minute,2,0),:,LPAD(_second,2,0)); -- 在这个日期上加上时分秒
   RETURN _myday;
END

带参数的函数

CREATE  FUNCTION `delete_t_mer_info_group`(`_uid` int) RETURNS int(11)
BEGIN
    #Routine body goes here...
DELETE from t_mer where uid=_uid;
DELETE from t_mer_info where uid=_uid;
DELETE from t_mer_group where member_uid =_uid or leader_uid=_uid;
    RETURN 0;


END

游标test

CREATE  PROCEDURE `curtest`()
BEGIN
DECLARE _uid INT;
DECLARE _miaohao VARCHAR(20);
DECLARE done INT DEFAULT FALSE;
 -- 遍历数据结束标志
DECLARE cur CURSOR FOR SELECT uid,miaohao FROM tmm_tmp_testaccount ;
  -- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  OPEN cur;
  read_loop: LOOP
    FETCH cur INTO _uid,_miaohao;
   IF done THEN
      LEAVE read_loop;
    END IF;
    UPDATE tmm_tmp_testaccount set `desc` = NULL where uid =_uid;
  END LOOP;
  CLOSE cur;
END

while循环

CREATE  PROCEDURE `add_t_statis_member_1`(in _member_uid int,in _leader_uid int,in _dayCount int)
BEGIN

DECLARE _count INT;
DECLARE _statis_date VARCHAR(20);

DELETE FROM t_statis_member_1 where member_uid = _member_uid;

SET _count = _dayCount;

WHILE _count > 0 DO
set _statis_date = date_format(DATE_SUB(NOW(),INTERVAL +_count DAY), %Y-%m-%d);



insert into t_statis_member_1(statis_date,member_uid,new_platform_user,new_game_user,active_user,pay_user,total_pay,total_order_price,leader_uid)
 values 
(
_statis_date,/*日期_varchar(16)_NO_statis_date*/
_member_uid,/*组员uid_bigint(20)_YES_member_uid*/
1,/*新增注册_int(11) unsigned zerofill_NO_new_platform_user*/
2,/*新增游戏注册_int(11)_NO_new_game_user*/
3,/*活跃人数_int(11)_NO_active_user*/
4,/*付费人数_int(11)_NO_pay_user*/
5.01,/*总付费金额_decimal(10,2)_NO_total_pay*/
6.01,/*总订单金额_decimal(10,2)_NO_total_order_price*/
_leader_uid /*所属组长uid_bigint(20)_YES_leader_uid*/);


set _count =_count-1;

END WHILE;

select * from t_statis_member_1 where member_uid = _member_uid;

END

拼接时间时分秒

CONCAT(_myday, ,LPAD(_hour,2,0),:,LPAD(_minute,2,0),:,LPAD(_second,2,0)); -- 在这个日期上加上时分秒

生成随机ID

CREATE FUNCTION `get_id`() RETURNS bigint(20)
BEGIN
    #Routine body goes here...

    RETURN CONCAT(UNIX_TIMESTAMP(NOW())-FLOOR(RAND() * 10000),FLOOR(RAND() * 10));
END

 

MYSQL 存储过程通用

标签:随机   时间   _for   insert   class   通用   val   col   rand   

原文地址:https://www.cnblogs.com/milanmi/p/8718194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!