gpt4 book ai didi

MySQL 游标问题

转载 作者:行者123 更新时间:2023-11-29 03:14:31 25 4
gpt4 key购买 nike

我得到了以下代码 - 这是我第一次真正尝试使用游标。

DELIMITER $$
DROP PROCEDURE IF EXISTS demo$$
DROP TABLE IF EXISTS temp$$

CREATE TEMPORARY TABLE temp(
id INTEGER NOT NULL AUTO_INCREMENT,
start DATETIME NOT NULL,
end DATETIME NOT NULL,
PRIMARY KEY(id)
) $$

CREATE PROCEDURE demo()
BEGIN

DECLARE done INT DEFAULT 0;
DECLARE a, b DATETIME;
DECLARE cur1 CURSOR FOR SELECT MAX(end) AS end
FROM (
SELECT id, start,
end,
@r := @r + (start > @edate) AS num,
@edate := GREATEST(@edate, end)
FROM (
SELECT @r := 0,
@edate := '0001-01-01'
) vars,
student_lectures
WHERE (
student_id = 1 AND
start >= '2010-04-26 00:00:00' AND
end <= '2010-04-30 23:59:59'
)
ORDER BY
start
) q
GROUP BY num;

DECLARE cur2 CURSOR FOR SELECT MIN(start) AS start
FROM (
SELECT id, start,
end,
@r := @r + (start > @edate) AS num,
@edate := GREATEST(@edate, end)
FROM (
SELECT @r := 0,
@edate := '0001-01-01'
) vars,
student_lectures
WHERE (
student_id = 1 AND
start >= '2010-04-26 00:00:00' AND
end <= '2010-04-30 23:59:59'
)
ORDER BY
start
) q
GROUP BY num
LIMIT 1, 18446744073709551615;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur1;
OPEN cur2;

REPEAT
FETCH cur1 INTO a;
FETCH cur2 INTO b;
IF NOT done THEN
INSERT INTO temp(start, end) VALUES(a,b);
END IF;
UNTIL done END REPEAT;

CLOSE cur1;
CLOSE cur2;

END $$

SELECT * FROM temp;

我没有将任何内容输出到 temp 表中。运行以下查询会得到输出,所以我知道它应该匹配一些行 - 但我想我犯了一些明显的错误。

SELECT MAX(end) AS end
FROM (
SELECT id, start,
end,
@r := @r + (start > @edate) AS num,
@edate := GREATEST(@edate, end)
FROM (
SELECT @r := 0,
@edate := '0001-01-01'
) vars,
student_lectures
WHERE (
student_id = 1 AND
start >= '2010-04-26 00:00:00' AND
end <= '2010-04-30 23:59:59'
)
ORDER BY
start
) q
GROUP BY num;

此查询返回的输出:

+---------------------+
| end |
+---------------------+
| 2010-04-26 13:00:00 |
| 2010-04-26 18:15:00 |
| 2010-04-27 11:00:00 |
| 2010-04-27 13:00:00 |
| 2010-04-27 18:15:00 |
| 2010-04-28 13:00:00 |
| 2010-04-29 13:00:00 |
| 2010-04-29 18:15:00 |
| 2010-04-30 13:00:00 |
| 2010-04-30 15:15:00 |
| 2010-04-30 17:15:00 |
+---------------------+
11 rows in set (0.02 sec)

最佳答案

你是在定义过程后调用它吗?如:CALL demo();

关于MySQL 游标问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2836782/

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