简体   繁体   English

MySQL 存储过程,多个 SELECT 只返回一行?

[英]MySQL Stored Procedure, Multiple SELECTs only return one row?

I've been working on a stored procedure that runs a select statement in a loop.我一直在研究一个在循环中运行 select 语句的存储过程。

When viewing results through mysqli or phpmyadmin, I only receive one row.通过mysqli或phpmyadmin查看结果时,我只收到一行。 What do I need to do to return multiple rows?我需要做什么才能返回多行?

Here's a really simple example that illustrates my problem....这是一个非常简单的例子,说明了我的问题......

DROP PROCEDURE IF EXISTS simple //
CREATE PROCEDURE simple()
BEGIN

DECLARE c INT(10);
SET c = 1;

REPEAT

  SELECT c;
  SET c = c + 1;

UNTIL c >= 10 END REPEAT;

END //

I think the best way to handle this would actually be to store your output into a temporary table, and then do a final select at the end of the while loop.我认为处理这个问题的最好方法实际上是将您的输出存储到一个临时表中,然后在 while 循环结束时进行最终选择。

DROP PROCEDURE IF EXISTS simple //
CREATE PROCEDURE simple()
BEGIN

    CREATE TEMPORARY TABLE output (finalC INT(10));

    DECLARE c INT(10);
    SET c = 1;

    REPEAT

      INSERT INTO output SELECT c;
      SET c = c + 1;

    UNTIL c >= 10 END REPEAT;

    SELECT * FROM output;

END //

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM