gpt4 book ai didi

mysql从循环中的选择查询返回结果集

转载 作者:行者123 更新时间:2023-11-29 15:55:34 25 4
gpt4 key购买 nike

我不是 mysql 存储过程专家。我需要从查询中返回元素的结果集。但是如果我调用这个过程,我只有一行,并且存储似乎被调用了n次。相反,我只想要一个在输出中包含许多行的调用存储过程。

这是存储的,请参阅评论以更好地了解我的目的

     DELIMITER //

DROP PROCEDURE IF EXISTS getExAnte //

CREATE PROCEDURE
getExAnte(startDate DateTime , stopDate DateTime)
BEGIN
DECLARE id INT ;
DECLARE instrumental TINYINT ;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE health_care_cursor CURSOR FOR SELECT h.id,h.instrumental FROM health_cares h inner join health_care_health_care_types hht on h.id=hht.health_care_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN health_care_cursor;

get_health_cares_loop: LOOP

IF done THEN
LEAVE get_health_cares_loop;
END IF;

FETCH health_care_cursor INTO id,instrumental;

############## I NEED TO ADD THIS QUERY RESULT TO SOME ARRAY OR CURSOR... HOW ?

SELECT distinct
count(*) as numTotPren ,
sum(case when appointment_date=availability_date then 1 else 0 end) as numTotPrenCheck,
sum(case when (appointment_date=availability_date AND urgency = 'B') then 1 else 0 end) as numB,
sum(case when (appointment_date=availability_date AND urgency = 'B' AND DATEDIFF(availability_date, contact_date) <= 10) then 1 else 0 end) as numBinTime,
sum(case when (appointment_date=availability_date AND urgency = 'D') then 1 else 0 end) as numD,
sum(case when (appointment_date=availability_date AND urgency = 'D' AND DATEDIFF(availability_date, contact_date) <= 30) then 1 else 0 end) as numDinTime

FROM cup_reservations
where
contact_date >=startDate and
contact_date <stopDate and
obsolete IS NULL and
health_care_code in (SELECT t.catalog_code FROM health_care_types t inner join health_care_health_care_types ht on t.id=ht.health_care_type_id where ht.health_care_id=id )

;

END LOOP get_health_cares_loop;

CLOSE health_care_cursor;
### NOW I NEED A RESULT SET OF ROWS TO RETURN

END
//

DELIMITER ;

最佳答案

MySQL中没有数组变量,但你可以使用临时表来存储部分结果并进行常规查询并将其用作“结果”

    DELIMITER //

DROP PROCEDURE IF EXISTS getExAnte //

CREATE PROCEDURE
getExAnte(startDate DateTime , stopDate DateTime)
BEGIN
DECLARE id INT ;
DECLARE instrumental TINYINT ;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE health_care_cursor CURSOR FOR SELECT h.id,h.instrumental FROM health_cares h inner join health_care_health_care_types hht on h.id=hht.health_care_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN health_care_cursor;

/*1: CREATE TEMPORARY TABLE*/
DROP TEMPORARY TABLE IF EXISTS results_table;
CREATE TEMPORARY TABLE results_table(
numTotPren int,
numTotPrenCheck int,
numB int,
numBinTime int,
numD int,
numDinTime int
);


get_health_cares_loop: LOOP

IF done THEN
LEAVE get_health_cares_loop;
END IF;

FETCH health_care_cursor INTO id,instrumental;

/*2: SAVE YOUR RESULTS IN TEMPORARY TABLE */
INSERT INTO results_table (numTotPren,numTotPrenCheck,numB,numBinTime,numD,numDinTime)
SELECT distinct
count(*) as numTotPren ,
sum(case when appointment_date=availability_date then 1 else 0 end) as numTotPrenCheck,
sum(case when (appointment_date=availability_date AND urgency = 'B') then 1 else 0 end) as numB,
sum(case when (appointment_date=availability_date AND urgency = 'B' AND DATEDIFF(availability_date, contact_date) <= 10) then 1 else 0 end) as numBinTime,
sum(case when (appointment_date=availability_date AND urgency = 'D') then 1 else 0 end) as numD,
sum(case when (appointment_date=availability_date AND urgency = 'D' AND DATEDIFF(availability_date, contact_date) <= 30) then 1 else 0 end) as numDinTime

FROM cup_reservations
WHERE
contact_date >=startDate and
contact_date <stopDate and
obsolete IS NULL and
health_care_code in (SELECT t.catalog_code FROM health_care_types t inner join health_care_health_care_types ht on t.id=ht.health_care_type_id where ht.health_care_id=id )

;

END LOOP get_health_cares_loop;

CLOSE health_care_cursor;

/*3: FINALLY RUN A REGULAR QUERY */
SELECT * FROM results_table;

END
//

DELIMITER ;

关于mysql从循环中的选择查询返回结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56497751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com