MySQL 游标问题

发布于 2024-09-01 10:29:21 字数 3036 浏览 6 评论 0原文

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

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)

I've got the following code - this is the first time I've really attempted using cursors.

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;

I'm not getting anything outputted into the temp table. Running the following query gives me output, so I know there's rows it should be matching - but I imagine I've made some obvious mistake.

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;

The output this query returns:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-09-08 10:29:21

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

Are you calling the procedure after defining it? as in: CALL demo();

公布 2024-09-08 10:29:21

我不确定,但是您不应该在插入后显式提交吗?

I am not sure, but should not you explicitly commit after insert?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文