mysql 多日誌表結果集合拼接儲存過程

2021-09-07 11:20:16 字數 1310 閱讀 6602

通常單天的日誌 僅僅記錄當天的日誌資訊,假設須要檢視一月內的日誌資訊須要對每天的日誌表結果集合進行拼接,通經常使用到 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

else

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')

union:聯合的意思,即把兩次或多次查詢結果合併起來。

要求:兩次查詢的列數必須一致

推薦:列的型別能夠不一樣,但推薦查詢的每一列,想相應的型別以一樣

能夠來自多張表的資料:多次sql語句取出的列名能夠不一致,此時以第乙個sql語句的列名為準。

假設不同的語句中取出的行,有全然同樣(這裡表示的是每乙個列的值都同樣),那麼union會將同樣的行合併,終於僅僅保留一行。也能夠這樣理解,union會去掉反覆的行。

假設不想去掉反覆的行,能夠使用union all 

Mysql 合併結果接橫向拼接字段

近日在做乙個報表功能裡面有乙個這樣的需求是統計各部門在某一月入職和離職的人數 我的步驟是這樣先查出入職的人數關鍵sql如下 select dept count 1 rcnumber from 員工表 where 入職時間 or 入職時間 is notnull and date format 入職時間...

mysql 表 日誌 檢視mysql的日誌

mysql日誌的種類,一般來說,日誌有五種,分別為 錯誤日誌 log err 記錄啟動,執行,停止mysql時出現的資訊 二進位制日誌 log bin 記錄所有更改資料的語句,還用於複製,恢復資料庫用 查詢日誌 log 記錄建立的客戶端連線和執行的語句 慢查詢日誌 log slow queries ...

MySQL查詢in操作 查詢結果按in集合順序顯示

引自 mysql 查詢in操作,查詢結果按in集合順序顯示 複製 如下 select from test where id in 3,1,5 order by find in set id,3,1,5 select from test where id in 3,1,5 order by subst...