- 使用遊標進行遍歷插入數據
-
DROP PROCEDURE IF EXISTS `queryMfrsStatistics`; CREATE PROCEDURE queryMfrsStatistics() begin -- 聲明一個標誌done, 用來判斷遊標是否遍歷完成 DECLARE done INT DEFAULT FALSE; -- 聲明一個變量,用來存放從遊標中提取的數據 -- 特別注意這裏的名字不能與由遊標中使用的列明相同,否則得到的數據都是NULL DECLARE a1 bigint(10) DEFAULT NULL; DECLARE a2 varchar(255) DEFAULT NULL; DECLARE a3 bigint(20) DEFAULT NULL; DECLARE a4 bigint(20) DEFAULT NULL; DECLARE a5 int(8) DEFAULT NULL; DECLARE a6 int(8) DEFAULT null; DECLARE a7 int(8) DEFAULT NULL; DECLARE a8 varchar(50) DEFAULT NULL; DECLARE a9 datetime DEFAULT NULL; -- id,mfrs_name, mfrs_table_sum,mfrs_tablefield_sum,fk_province,fk_city,fk_district,address,statistics_create -- 聲明遊標對應的 SQL 語句 DECLARE cur CURSOR FOR SELECT -- g.aid as id, null, g.mfrs_name as mfrs_name, sum(e.f) as mfrs_table_sum, sum(e.x) as mfrs_tablefield_sum, g.aprovince as fk_province , g.acity as fk_city, g.adistrict as fk_district, g.aaddress as address, now() as statistics_create FROM ( SELECT a.id aid, a.mfrs_name, a.fk_province aprovince, a.fk_city acity, a.fk_district adistrict, a.address aaddress, b.id bid FROM t_mfrs_dic a LEFT JOIN t_mfrs_system b ON a.id = b.fk_mfrs_id ) AS g LEFT JOIN ( SELECT z.fk_mfrs_system_id, z.f, y.x FROM ( SELECT fk_mfrs_system_id, count(1) f FROM t_mfrs_table_dict GROUP BY fk_mfrs_system_id ) z LEFT JOIN ( SELECT h.fk_mfrs_system_id, sum(h.l) x FROM ( SELECT m.fk_mfrs_system_id, m.id j, n.id k, count(1) l FROM t_mfrs_table_dict m LEFT JOIN t_mfrs_tablefield n ON m.id = n.fk_mfrs_table_id GROUP BY m.id ) h GROUP BY h.fk_mfrs_system_id ) y ON z.fk_mfrs_system_id = y.fk_mfrs_system_id ) e ON g.bid = e.fk_mfrs_system_id GROUP BY g.aid ; -- 在遊標循環到最後會將 done 設置爲 TRUE DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- 執行查詢 open cur; -- 遍歷遊標每一行 read_loop: LOOP -- 提取遊標裏的數據 FETCH cur INTO a1,a2,a3,a4,a5,a6,a7,a8,a9; -- 聲明結束的時候 IF done THEN LEAVE read_loop; END IF; INSERT INTO t_mfrs_statistics_table(id, mfrs_name, mfrs_table_sum, mfrs_tablefield_sum, fk_province, fk_city, fk_district, address,statistics_create) VALUES(a1,a2,a3,a4,a5,a6,a7,a8,a9); END LOOP; -- 關閉遊標 CLOSE cur; end -- 調用存儲過程 -- call test3() -- mysql 定時任務 CREATE EVENT if not exists queryMfrsStatistics on schedule every 604800 second on completion preserve do call queryMfrsStatistics();
使用以下簡單方法進行 數據插入
-
-- 醫療機構存儲過程 drop PROCEDURE queryMidStatistics; CREATE PROCEDURE queryMidStatistics() BEGIN insert into t_mid_statistics_table(medical_id,medical_org_name,fk_province,fk_city,fk_district,address) SELECT id, medical_org_name, fk_province, fk_city, fk_district, address FROM t_ds_org; COMMIT; update t_mid_statistics_table tmp set tmp.mid_table_sum = ( select a.num_ from (select max(m.id) as id, count(*) as num_ from t_mid_statistics_table m right JOIN t_org_system s on(s.fk_org_id = m.medical_id) right JOIN t_org_database d on(d.fk_org_system_id = s.id) right join t_ds_table t on(t.fk_belong_datatable = d.id) where m.`status` = 0 GROUP BY m.medical_id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mid_statistics_table tmp set tmp.mid_sign_table_sum = ( select a.num_ from (select max(m.id) as id, count(*) as num_ from t_mid_statistics_table m right JOIN t_org_system s on(s.fk_org_id = m.medical_id) right JOIN t_org_database d on(d.fk_org_system_id = s.id) right join t_ds_table t on(t.fk_belong_datatable = d.id and t.table_type = 1) where m.`status` = 0 GROUP BY m.medical_id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mid_statistics_table tmp set tmp.mid_tablefield_sum = ( select a.num_ from (select max(m.id) as id, count(*) as num_ from t_mid_statistics_table m right JOIN t_org_system s on(s.fk_org_id = m.medical_id) right JOIN t_org_database d on(d.fk_org_system_id = s.id) right join t_ds_table t on(t.fk_belong_datatable = d.id) right join t_ds_tablefield f on(f.fk_belong_table = t.id) where m.`status` = 0 GROUP BY m.medical_id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mid_statistics_table tmp set tmp.mid_sign_tablefield_sum = ( select a.num_ from (select max(m.id) as id, count(*) as num_ from t_mid_statistics_table m right JOIN t_org_system s on(s.fk_org_id = m.medical_id) right JOIN t_org_database d on(d.fk_org_system_id = s.id) right join t_ds_table t on(t.fk_belong_datatable = d.id) right join t_ds_tablefield f on(f.fk_belong_table = t.id and f.sign = 1) where m.`status` = 0 GROUP BY m.medical_id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mid_statistics_table tmp set tmp.mid_sign_metadata_sum = ( select a.num_ from (select max(m.id) as id, count(*) as num_ from t_mid_statistics_table m right JOIN t_org_system s on(s.fk_org_id = m.medical_id) right JOIN t_org_database d on(d.fk_org_system_id = s.id) right join t_ds_table t on(t.fk_belong_datatable = d.id) right join t_ds_tablefield f on(f.fk_belong_table = t.id) right join t_dc_ref r on(r.fk_ds_id = f.id) where m.`status` = 0 GROUP BY m.medical_id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mid_statistics_table tmp set tmp.mid_mapping_dataset_table_sum = ( select a.num_ from (select m.id, count(*) as num_ from ( select max(m.id) as id, t.id as table_id, count(*) as num_ from t_mid_statistics_table m right JOIN t_org_system s on(s.fk_org_id = m.medical_id) right JOIN t_org_database d on(d.fk_org_system_id = s.id) right join t_ds_table t on(t.fk_belong_datatable = d.id) right join t_ds_tablefield f on(f.fk_belong_table = t.id) right join t_dc_ref r on(r.fk_ds_id = f.id and r.fk_dataset_item_id is not null) where m.`status` = 0 GROUP BY m.medical_id, t.id) m group by m.id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; -- 修改 update t_mid_statistics_table tmp set tmp.mid_mapping_dataset_sum = ( select a.num_ from (select m.id, count(*) as num_ from ( select max(m.id) as id, t.id as table_id, count(*) as num_ from t_mid_statistics_table m right JOIN t_org_system s on(s.fk_org_id = m.medical_id) right JOIN t_org_database d on(d.fk_org_system_id = s.id) right join t_ds_table t on(t.fk_belong_datatable = d.id) right join t_ds_tablefield f on(f.fk_belong_table = t.id) right join t_dc_ref r on(r.fk_ds_id = f.id and r.fk_dataset_item_id is not null) right join t_ds_comfield cd ON (cd.fk_tablefield_id = t.id AND cd.fk_tablefield_id is not null) where m.`status` = 0 GROUP BY m.medical_id, t.id) m group by m.id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mid_statistics_table set status = 1 where `status` = 0; COMMIT; END -- HIT廠商的存儲過程 begin insert into t_mfrs_statistics_table(mfrs_id,mfrs_name,fk_province,fk_city,fk_district,address) SELECT id, mfrs_name, fk_province, fk_city, fk_district, address FROM t_mfrs_dic; COMMIT; update t_mfrs_statistics_table tmp set tmp.mfrs_table_sum = ( select a.num_ from (select max(m.id) as id, count(*) as num_ from t_mfrs_statistics_table m right JOIN t_mfrs_system s on(s.fk_mfrs_id = m.mfrs_id) right join t_mfrs_table_dict t on(t.fk_mfrs_system_id = s.id) where m.`status` = 0 GROUP BY m.mfrs_id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mfrs_statistics_table tmp set tmp.mfrs_tablefield_sum = ( select a.num_ from (select max(m.id) as id, count(*) as num_ from t_mfrs_statistics_table m right JOIN t_mfrs_system s on(s.fk_mfrs_id = m.mfrs_id) right join t_mfrs_table_dict t on(t.fk_mfrs_system_id = s.id) right join t_mfrs_tablefield f on(f.fk_mfrs_table_id = t.id) where m.`status` = 0 GROUP BY m.mfrs_id) a where a.id = tmp.id) where tmp.`status` = 0; COMMIT; update t_mfrs_statistics_table set status = 1 where `status` = 0; COMMIT; end
以上屬於公司自己的業務,只爲了筆記,如果看的不懂,勿噴,謝謝
mysql定時任務與查詢多表數據存入另一張表中
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.