【问题标题】:How to excute select query in side store procedure in php mysql if there are multiple rows occurs?如果发生多行,如何在 php mysql 的侧存储过程中执行选择查询?
【发布时间】:2013-10-11 14:26:34
【问题描述】:

如果出现多行,如何在php mysqlstore procedure中执行select query

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

然后代表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// 

【问题讨论】:

    标签: php mysql sqlite


    【解决方案1】:

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

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

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

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

    您需要使用 DECLARE 语句修改以上内容以获取本地 chapterid 变量。

    您也不能在 MySQL 存储过程中使用 chpapter_successrate[i] 作为数组。请参阅:How can I simulate an array variable in MySQL? 了解替代方案。也许临时表是一个很好的解决方法。

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2013-03-12
      • 2014-06-17
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多