mysql 根据select结果进行循环操作,并写入临时表
数据库中有一个表:如下
SELECT id, descs FROM table_learn WHERE id>0 and id<4;
结果如下:
下面是一个demo,可以直接按照这个结果循环操作:
delimiter //
drop procedure if exists sp_name //
create procedure sp_name()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE v_id int;
DECLARE v_descs varchar(100);
#declare cursor
DECLARE cur1 CURSOR FOR SELECT id, descs
FROM table_learn
WHERE id>0 and id<4;
#declare handle
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
drop table if exists tmp_table;
create temporary table tmp_table(id int, descs varchar(100));
#open cursor
OPEN cur1;
#starts the loop
the_loop: LOOP
#get the values of each column into our variables
FETCH cur1 INTO v_id,v_descs;
IF done THEN
LEAVE the_loop;
END IF;
#Do some post processing
IF v_id IS NOT NULL THEN
SET v_descs = "haha";
END IF;
#Insert it
INSERT INTO tmp_table (id, descs)
VALUES (v_id, v_descs);
END LOOP the_loop;
CLOSE cur1;
select * from tmp_table;
end;
//
delimiter ;
call sp_name();
更多相关文章
- MySQL基本操作汇总
- MySQL修改表结构操作命令总结
- mysql主从复制配置操作以及主主配置宕机切换演练
- MySQL在Linux下Shell脚本操作命令
- 不知道有人在mysql5.0上 针对10亿条数据操作过没有
- linux环境下写C++操作mysql(一)
- iframe操作、调用父页面元素或js函数
- 11、javascript中字符串常用操作总结、JS字符串操作大全
- javascript应该注意的小case--操作符