mysql 存儲過程
問題描述
1.問題描述:我想要從一個表中查找數(shù)據(jù),然后取其中一些字段批量存到另一張表中,剛學習用存儲過程,但是一直顯示影響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這個表取數(shù)據(jù)插入edu_course_history這個表么?1.先確認下有沒有數(shù)據(jù):
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.試試執(zhí)行能成功么?
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 ...
相關(guān)文章:
1. javascript - sublime快鍵鍵問題2. javascript - immutable配合react提升性能?3. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務器還是不能訪問?4. javascript - 移動端上不能實現(xiàn)拖拽布局嗎?5. 實現(xiàn)bing搜索工具urlAPI提交6. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化7. javascript - nodejs關(guān)于進程間發(fā)送句柄的一點疑問8. phpstudy8.1支持win11系統(tǒng)嗎?9. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?10. css - 寫頁面遇到個布局問題,求大佬們幫解答,在線等,急!~

網(wǎng)公網(wǎng)安備