English 中文(简体)
选择内部插入语句 - MySQL 光标
原标题:Select Inside Insert Statement - MySQL Cursors

我尝试过一些, 但我无法找到解决办法, 不知怎的,我设法得到了这个结果。

以下是查询 :

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   |
+----------+---------+-------+

I m getting (NULL) for staff_id, How can I get the staff_id s there ? I tried using declared variable a in insert statement, but at that time I got only staff_id, I didn t get the other 2 fields, I can t get the staff_id from the select inside insert statement coz there is some problem.

现在我需要的是,我需要把变量a 的工作人员代号插入到临时表格中。

注意: 我对这个存储程序非常新, 但到这里为止, 有了管理, 如果能详细了解如何在“ 插入” 中使用“ 选择 ”, 包括这个解决方案, 就会更好 。

最佳回答

试试这个

SELECT a, (COUNT(I.interval_start)) AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1     DAY),start_date) AS total
问题回答

您的 INSERT 需要三个字段, 但您的 SELECT 语句只选择两个 : present commont

尝试 :

 SELECT E.staffid, (COUNT(I.interval_start)) 
 AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1 DAY),start_date) AS total




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签