mysql 存储过程
问题描述
1.问题描述:我想要从一个表中查找数据,然后取其中一些字段批量存到另一张表中,刚学习用存储过程,但是一直显示影响0行,不知道是哪里错了,请各位帮帮忙。谢谢了!2.代码:
BEGIN #Routine body goes here... DECLARE id INT DEFAULT 0; DECLARE user_Id INT DEFAULT 0; DECLARE course_id INT DEFAULT 0; DECLARE playercount INT DEFAULT 0; DECLARE course_name VARCHAR(255); DECLARE play_time INT DEFAULT 0; DECLARE finish INT DEFAULT 0; DECLARE _done TINYINT(1) DEFAULT 0; DECLARE mian_cur CURSOR FOR SELECT user_id,course_id,SUM(playercount) AS playercount,SUM(play_time) AS play_time,course_name FROM `edu_course_studyhistory` WHERE user_id = userId GROUP BY course_id ; DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = 1; OPEN mian_cur; loop_xxx:LOOP FETCH FROM mian_cur INTO user_id,course_id,playercount,play_time,course_name;IF _done=1 THEN LEAVE loop_xxx; END IF;INSERT INTO edu_course_history VALUES(NULL,user_id,course_id,playercount,course_name,now(),play_time,0); END LOOP;END
3.错误信息
问题解答
回答1:从这个edu_course_studyhistory这个表取数据插入edu_course_history这个表么?1.先确认下有没有数据:
SELECT user_id, course_id, SUM(playercount) AS playercount, SUM(play_time) AS play_time, course_nameFROM `edu_course_studyhistory`WHERE user_id = userIdGROUP BY course_id;
2.试试执行能成功么?
INSERT INTO edu_course_historyVALUES (NULL,user_id,course_id,playercount,course_name,now(),play_time,0 );回答2:
看起来没必要用存储过程吧?维护起来还麻烦。
INSERT INTO table1(field1, field2, ...)SELECT field1, field2 FROM table2WHERE ...
相关文章:
1. docker容器呢SSH为什么连不通呢?2. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””3. docker网络端口映射,没有方便点的操作方法么?4. nignx - docker内nginx 80端口被占用5. debian - docker依赖的aufs-tools源码哪里可以找到啊?6. 前端 - ng-view不能加载进模板7. android clickablespan获取选中内容8. python - from ..xxxx import xxxx到底是什么意思呢?9. javascript - iframe 为什么加载网页的时候滚动条这样显示?10. angular.js - ng-grid 和tabset一起用时,grid width默认特别小
