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 ...
相关文章: