mysql 從相同類型的多張表中提取到一張表中

來源:互聯網
上載者:User

標籤:rom   oop   close   creat   for   incr   取資料   not found   als   

蝸牛背著沉重的殼,貼著地面一步步艱難地向前爬行,不回頭,也不左顧右盼,只是朝著自己想到達的地方行進。

有時候需要從多張相同類型的表中提取資料,這些表有一些相同的列或者表結構完全相同,同時表名存在一定的規律,如果表數量少還好,如果表數量多的話則會比較繁瑣。可以通過預存程序將多張表的資料提取到一張表的方法來降低工作量。

先建立測試表並產生測試資料。
以下預存程序建立10張測試表,每張表產生10條測試資料。
drop PROCEDURE if EXISTS create10tables;
create PROCEDURE create10tables()
BEGIN
DECLARE t_name VARCHAR(32);
DECLARE i INT;
DECLARE j INT;
DECLARE continue HANDLER for not found set t_name = "";
set i = 0;
set j = 0;
create_loop:LOOP
set i = i + 1;
set t_name = CONCAT("ttest",i);
set @dropsql = CONCAT(‘drop table if EXISTS ‘,t_name);
#select @dropsql;
prepare dropsql from @dropsql;
EXECUTE dropsql;
DEALLOCATE prepare dropsql;
set @createsql = concat(‘create table ‘,t_name,‘ (id int(11) not null auto_increment,modifytime timestamp null default current_timestamp,vdata varchar(32) ,primary key(id));‘);
#select @createsql;
prepare createsql from @createsql;
EXECUTE createsql;
DEALLOCATE prepare createsql;
insert_loop:LOOP
set j = j+1;
if j > 10 THEN
LEAVE insert_loop;
end if;
set @insertsql = concat(‘insert into ‘,t_name,‘(vdata) values(md5(rand()));‘);
#select @insertsql;
PREPARE insertsql from @insertsql;
EXECUTE insertsql;
deallocate PREPARE insertsql;
end LOOP insert_loop;
set j = 0;
if i > 10 THEN
LEAVE create_loop;
end if;
end LOOP create_loop;
END;
#執行create10tables 產生表與資料
call create10tables();

有了表和資料,可以在information_schema資料庫的innodb_tables表中看到新產生的對應的表名。這裡可以通過該表將所有的表名查出來,然後進行資料提取。SQL如下:
drop PROCEDURE if exists selectalldata;
drop table if exists t_test;
create PROCEDURE selectalldata()
BEGIN
DECLARE done int DEFAULT FALSE;
DECLARE t_name VARCHAR(32);
declare i int;
DECLARE cur1 CURSOR for SELECT table_name from information_schema.TABLES where table_name like "ttest%";
DECLARE continue HANDLER for not found set done = TRUE;
open cur1;
set i = 0;
read_loop:LOOP
fetch cur1 into t_name;
if done THEN
LEAVE read_loop;
end if;
if i = 0 THEN
set @createsql = concat(‘create table t_test (id int(11) not null auto_increment,modifytime timestamp null default current_timestamp,vdata varchar(32) ,primary key(id));‘);
#select @createsql;
prepare createsql from @createsql;
EXECUTE createsql;
DEALLOCATE prepare createsql;
set i = i + 1;
end if;
set @insertsql = concat(‘insert into t_test(modifytime,vdata) select modifytime,vdata from ‘,t_name);
prepare insertsql from @insertsql;
EXECUTE insertsql;
DEALLOCATE prepare insertsql;
end LOOP;
close cur1;
END;
#執行selectdata
call selectalldata();
執行之後可以在t_test表中看到已經將所有的資料都提取出來了。

實際操作中,根據需要修改對應的SQL語句即可。

mysql 從相同類型的多張表中提取到一張表中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.