通常单天的日志 只记录当天的日志信息,如果需要查看一月内的日志信息需要对每天的日志表结果集合进行拼接,通常用到 union 。 储存过程: drop PROCEDURE if EXISTS unionSp; DELIMITER // create procedure unionSp(sTime varchar(32), eTime varchar(32),tchema varchar(32)) begin declare sqlVar varchar(1024000); declare rest int; declare tableName varchar(1024); set rest = 100; set sqlVar=''; while rest > 0 do set sTime = (select DATE_FORMAT((select ADDDATE(sTime,1)),'%Y%m%d')); set tableName=CONCAT('tbl_req_',sTime); select count(1) from information_schema.tables where table_name = tableName and TABLE_SCHEMA=tchema into @cnt; if @cnt != 0 then if rest=1 then set sqlVar=CONCAT(sqlVar,' SELECT DISTINCT channel_id,app_id from tbl_req_',sTime); ELSE set sqlVar=CONCAT(sqlVar,' SELECT DISTINCT channel_id,app_id from tbl_req_',sTime,' UNION'); END IF; END if; set rest = DATEDIFF(eTime,sTime); END while; set @v_s=sqlVar; prepare stmt from @v_s; EXECUTE stmt; DEALLOCATE PREPARE stmt; end; // DELIMITER; call unionSp('20140730','20140930','biz_date')
|