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. nginx - 关于javaweb项目瘦身问题,前期开发后,发现项目占用存贮空间太大,差不多1.2个G,怎么实现瘦身,动态页面主要是jsp。2. angular.js - angular内容过长展开收起效果3. mysql - 这条sql语句为什么加上where就报错?4. docker网络端口映射,没有方便点的操作方法么?5. android-studio - Android Studio编译出错6. javascript - 三目运算符的一些问题7. angular.js - angular指令中的scope属性中用&获取父作用域函数的问题8. nginx反向代理配置访问后台,基础问题?9. 请教关于jquery阻止冒泡的问题?10. 前端HTML与PHP+MySQL连接

网公网安备