4

如果出现多行,如何在php mysql中执行select query内部存储过程?

首先我想distinct (chapter_id)a table (practice_test_result)特定学生那里获取

然后代表该distinct(chapter_id )运行一个查询以生成关联数组

我面临的问题是:

while( row found ){
/* here i perform the SELECT SUM(total_question) - SUM(total_incomplete) , SUM(total_correct) into totalattempt,talcorrect FROM practice_test_summary WHERE student_id =studentid AND chapter_id =chapterid;
set chpapter_successrate[i] =(totalcorrect * 100)/totalattempt; */

}

下面的代码了解更多关于我到底想要什么的问题

DELIMITER //
DROP PROCEDURE IF EXISTS get_result//
CREATE PROCEDURE get_result(studentid INT,courseid INT,out chpapter_successrate decimal(10,2))
BEGIN


SELECT distinct(chapter_id)  FROM practice_test_result WHERE course_id =courseid AND student_id =studentid;
set chapterid=chapter_id;
//here i want to use while loop for all chapter_id  and behalf of these chpater id's perform another select statement for performing successrate of specific chapter and store that into an array and return that array as out variable

SELECT SUM(total_question) - SUM(total_incomplete) , SUM(total_correct) into totalattempt,talcorrect FROM practice_test_summary WHERE student_id =studentid AND chapter_id =chapterid;

set chpapter_successrate[i] =(totalcorrect * 100)/totalattempt;


END// 
4

1 回答 1

0

您想要用来生成 while 循环功能的是 mysql“游标”。

然后,您可以将每个 chapter_id “获取”到一个局部变量中,以在您的第二个 select 语句中使用。

这是 mysql 文档的链接和一个很好的示例。

http://dev.mysql.com/doc/refman/5.0/en/cursors.html

您需要使用 DECLARE 语句为本地 chapterid 变量修改上述内容。

此外,您将无法在 MySQL 存储过程中使用 chpapter_successrate[i] 作为数组。请参阅:如何在 MySQL 中模拟数组变量?为替代品。也许临时表将是一个很好的解决方法。

于 2015-03-20T15:07:43.943 回答