0

我尝试了一些,但我找不到解决方案,不知何故我设法得到了这个结果。

这是查询:

DELIMITER ##
CREATE PROCEDURE test1(start_date DATE,end_date DATE)
BEGIN
   DECLARE done INT DEFAULT FALSE;   
   DECLARE a INT;
   DECLARE present INT;
   DECLARE total INT;
   -- Declare the cursor
   DECLARE id CURSOR
   FOR
   SELECT staff_id FROM ost_staff;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
   -- Open the cursor
   DROP TEMPORARY TABLE IF EXISTS reports;
   CREATE TEMPORARY TABLE IF NOT EXISTS reports
   (
    staff_id INT(10),
    present INT(10),
total INT(10)
   );
   OPEN id;
   read_loop: LOOP
   FETCH id INTO a;
   IF done THEN
   LEAVE read_loop;
   END IF;
   INSERT INTO reports(staff_id,present,total)
   SELECT (COUNT(I.interval_start)) AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1     DAY),start_date) AS total
    FROM effort_frequency E 
RIGHT OUTER JOIN time_intervals I ON I.interval_start = E.log_date 
AND E.staffid=a AND E.log_date BETWEEN start_date AND end_date
LEFT OUTER JOIN ost_holidays H ON H.holiday_date = I.interval_start
WHERE DATE_FORMAT(I.interval_start,'%a') = 'Sun' OR H.holiday_date = I.interval_start OR E.total_effortspent IS NOT NULL;

   -- Close the cursor   
   END LOOP;
   CLOSE id;
END ##

我得到以下结果:

+----------+-----------------+
| staff_id | present | total |
+----------+---------+-------+
|  (NULL)  |   23    |  24   |
|  (NULL)  |   22    |  24   |
+----------+---------+-------+

我正在为 staff_id 获取(NULL),我怎样才能获得 staff_id?我尝试在插入语句中使用声明的变量'a',但当时我只得到了staff_id,我没有得到其他2个字段,我无法从select inside insert语句中获取staff_id,因为有一些问题。

现在我需要将变量'a'中的staff_id插入到该临时表中。

注意:我对这个存储过程真的很陌生,但不知何故管理到这里,如果我能得到一些关于如何在插入中使用 Select 的详细信息,包括解决方案,那就太好了。

4

2 回答 2

1

尝试这个 -

SELECT a, (COUNT(I.interval_start)) AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1     DAY),start_date) AS total
于 2012-05-25T09:49:27.347 回答
0

INSERT需要三个字段,但您的SELECT语句仅选择两个:presenttotal.

尝试:

 SELECT E.staffid, (COUNT(I.interval_start)) 
 AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1 DAY),start_date) AS total
于 2012-05-25T09:45:43.900 回答