1、用游標循環
BEGIN
-- 定義變量
-- 定義done
DECLARE done INT;
-- 定義 ammeter_id_bl
DECLARE ammeter_id_bl DOUBLE;
-- 定義表名(tableName)游標
DECLARE rs_ammeter_id CURSOR FOR
-- 得到游標集合
SELECT id FROM `res_meter` WHERE id<>1 AND id<>10 AND meter_type=1;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
-- 初始化done,為0,false
SET done = 0;
-- 打開游標
OPEN rs_ammeter_id;
-- 遍歷游標(開始循環)
REPEAT
FETCH rs_ammeter_id into ammeter_id_bl;
IF done<>1 then
/*
1、處理要插入的數據
*/
-- 給變量賦值
SET @ammeter_id_bl=ammeter_id_bl;
SELECT t.*
INTO
@min_meter_count_top
,@min_meter_count_max
,@min_meter_count_avg
,@min_meter_count_min
,@add_top
,@add_max
,@add_avg
,@add_min
,@collect_time
,@meter_status
FROM
(
SELECT
meter_count_top
,meter_count_max
,meter_count_avg
,meter_count_min
,ROUND(RAND()*10+4,2) add_top
,ROUND(RAND()*8+3,2) add_max
,ROUND(RAND()*7+2,2) add_avg
,ROUND(RAND()*6+1,2) add_min
,DATE_ADD(collect_time, INTERVAL 30 MINUTE) collect_time
,ROUND(RAND(),0) meter_status
FROM
cap_ammeter_201810
WHERE
collect_time = (
SELECT
MAX(collect_time) collect_time
FROM
cap_ammeter_201810
WHERE
ammeter_id = @ammeter_id_bl
GROUP BY
ammeter_id
)
AND ammeter_id = @ammeter_id_bl
)t;
SET @meter_count_top=ROUND(@min_meter_count_top+@add_top,2)
,@meter_count_max=ROUND(@min_meter_count_max+@add_max,2)
,@meter_count_avg=ROUND(@min_meter_count_avg+@add_avg,2)
,@meter_count_min=ROUND(@min_meter_count_min+@add_min,2);
SET @meter_count=ROUND(@meter_count_top+@meter_count_max+@meter_count_avg+@meter_count_min,2);
-- 查看變量的值
/*
SELECT
@ammeter_id_bl
,@collect_time
,@meter_status
,@min_meter_count_top
,@min_meter_count_max
,@min_meter_count_avg
,@min_meter_count_min
,@add_top
,@add_max
,@add_avg
,@add_min
,@meter_count_top
,@meter_count_max
,@meter_count_avg
,@meter_count_min;
*/
/*
2、插入數據
*/
-- 1)寫sql語句 如果要用到變量,使用CONCAT()拼接
-- 查詢出要插入的數據
SET @queryDataSqlStr=CONCAT("SELECT
",ROUND(@meter_count,2)," meter_count
,'",@collect_time,"' collect_time
,",@ammeter_id_bl," ammeter_id
,",@meter_status," meter_status
,",ROUND(@meter_count_top,2)," meter_count_top
,",ROUND(@meter_count_max,2)," meter_count_max
,",ROUND(@meter_count_avg,2)," meter_count_avg
,",ROUND(@meter_count_min,2)," meter_count_min"
);
-- 插入數據
SET @insertSqlStr=CONCAT("INSERT INTO cap_ammeter_201810
(
meter_count
,collect_time
,ammeter_id
,meter_status
,meter_count_top
,meter_count_max
,meter_count_avg
,meter_count_min
) ",@queryDataSqlStr
);
-- 4)查看sql語句
-- SELECT @insertSqlStr;
-- 3)執行sql語句
PREPARE insertSqlStr FROM @insertSqlStr;
EXECUTE insertSqlStr;
END IF;
-- 直到done變為true結束循環
UNTIL done END
REPEAT;
CLOSE rs_ammeter_id;
END
2、while循環
BEGIN
-- 定義變量
DECLARE i INT DEFAULT 10;
-- 開始循環
WHILE i<13
DO
SET @day_bl=i;
SET @createSqlStr=CONCAT("CREATE TABLE cap_ammeter_2017",@day_bl," (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`meter_count` double(14,2) DEFAULT NULL COMMENT '電表讀數',
`collect_time` datetime DEFAULT NULL COMMENT '采集時間',
`ammeter_id` bigint(20) NOT NULL COMMENT '電表id,對應res_meter的id',
`meter_status` int(1) NOT NULL COMMENT '電表狀態 0:正常;1:異常',
`meter_count_top` double(14,2) DEFAULT NULL COMMENT '尖值電量',
`meter_count_max` double(14,2) DEFAULT NULL COMMENT '峰值電量',
`meter_count_avg` double(14,2) DEFAULT NULL COMMENT '平值電量',
`meter_count_min` double(14,2) DEFAULT NULL COMMENT '谷值電量',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19576 DEFAULT CHARSET=utf8;");
-- 查看sql
-- SELECT @createSqlStr;
-- 運行sql
PREPARE createSqlStr FROM @createSqlStr;
EXECUTE createSqlStr;
SET i=i+1;
END WHILE;
COMMIT;
END
還可以看看
其他文章,謝謝您的閱讀。
網站申明:系本文編輯轉載,來源于網絡,目的在于傳遞更多信息,并不代表本網贊同其觀點和對其真實性負責,所有權歸屬原作者。如內容、圖片有任何版權問題,請
聯系我們刪除。