PROCEDURE 存储过程

DELIMITER $$

USE `paymgr` $$

DROP PROCEDURE IF EXISTS `proc_everday` $$

CREATE DEFINER = `root` @`%` PROCEDURE `proc_everday` () 
BEGIN
  DECLARE suffix CHAR(6) DEFAULT DATE_FORMAT(
    DATE_SUB(NOW(), INTERVAL 1 DAY),
    '%Y%m'
  ) ;
  DECLARE statdate DATE ;
  DECLARE tbl_name VARCHAR (50) ;
  DELETE 
  FROM
    `ccg_charge_day` 
  WHERE TO_DAYS(NOW()) - TO_DAYS(`stat_date`) = 1 ;
  SET tbl_name = CONCAT('ccg_charge_', suffix) ;
  SET statdate = DATE_FORMAT(
    DATE_SUB(NOW(), INTERVAL 1 DAY),
    '%Y-%m-%d'
  ) ;
  SET @stmt := CONCAT(
    "insert into `ccg_charge_day` (`total_count`, `success_count`, `fail_count`, `total_money`, `success_money`, `fail_money`, `stat_date`) SELECT c.total_count,cc.success_count,ccc.fail_count,c.total_money ,cc.success_money,ccc.fail_money,c.stat_date FROM (SELECT DATE_FORMAT(`updatetime`, '%Y-%m-%d') AS 'stat_date', COUNT(`id`) AS 'total_count',SUM(`amount`) AS 'total_money' FROM ",
    tbl_name,
    " WHERE DATE_FORMAT(`updatetime`, '%Y-%m-%d')='",
    statdate,
    "') c,(SELECT COUNT(`id`) AS 'success_count',SUM(`amount`) AS 'success_money' FROM ",
    tbl_name,
    " WHERE `status`=0 AND DATE_FORMAT(`updatetime`, '%Y-%m-%d')='",
    statdate,
    "') cc,(SELECT COUNT(`id`) AS 'fail_count',SUM(`amount`) AS 'fail_money' FROM ",
    tbl_name,
    " WHERE `status`<>0 AND DATE_FORMAT(`updatetime`, '%Y-%m-%d')='",
    statdate,
    "') ccc;"
  ) ;
  PREPARE stmt FROM @stmt ;
  EXECUTE stmt ;
  DEALLOCATE PREPARE stmt ;
END $$

DELIMITER ;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值