在mysql中获取存储过程的数组输出

wpcxdonn  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(476)

我已经创建了一个存储过程来计算每个技术人员的工作数量,使用该技术人员的id和预期年份作为存储过程的输入,但是我在。如何在该过程中获得输出数组,以便稍后在php代码中使用它。这是我的存储过程:

DROP PROCEDURE `work_count`;
CREATE DEFINER=`root`@`localhost` PROCEDURE `work_count`(IN `id` INT, IN `yearInput` INT) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
BEGIN 
    SET @x := 1; 
    WHILE @x<13 DO 
       SELECT COUNT(work.workType) 
       FROM work 
       WHERE year(workDate)=yearInput 
          AND work.technicianID = id 
          AND month(workDate)= @x; 
       SET @x := @x+1; 
    END WHILE; 
 END
hk8txs48

hk8txs481#

如果要执行多个查询,则需要创建一个临时表来保存中间结果,然后只需 SELECT * FROM thattemporarytable 你手术中最后做的一件事。
像这样的。。。

BEGIN
   DROP TEMPORARY TABLE IF EXISTS `blah` ( num INT );
   CREATE TEMPORARY TABLE `blah` ( num INT );
   SET @x := 1; 
   WHILE @x<13 DO 
      INSERT INTO blah (num)
      SELECT COUNT(work.workType) 
      FROM work 
      WHERE year(workDate)=yearInput 
            AND work.technicianID = id 
            AND month(workDate)= @x
      ; 
      SET @x := @x+1; 
   END WHILE; 
   SELECT * FROM blah;
   DROP TEMPORARY TABLE blah;
END

或者,如果可以将其作为单个查询进行管理,则正常的select应该可以工作:

BEGIN
   SELECT COUNT(work.workType) 
   FROM work 
   WHERE year(workDate)=yearInput 
         AND work.technicianID = id 
         AND month(workDate) BETWEEN 1 AND 12
   ;
END
tjjdgumg

tjjdgumg2#

我使用了一个临时表将数据存储到其中,然后使用select语句发布它,并在过程结束时删除临时表

BEGIN

  drop TABLE if exists `myDB`.`tbl_temp`;
  CREATE TABLE `myDB`.`tbl_temp` ( `countingWork` INT(11) NOT NULL ) ENGINE = InnoDB;   
  SET @x :=1;
  WHILE @x<=12 DO
      insert into `myDB`.`tbl_temp`
      SELECT COUNT(work.workType) 
      FROM work
      WHERE year(workDate)=yearInput 
            AND work.technicianID = id 
            AND month(workDate)= @x;
      SET @x = @x + 1;
  END WHILE;
  SELECT * from `myDB`.`tbl_temp`;
  drop TABLE `myDB`.`tbl_temp`;
END

相关问题