MySQL 游标问题
我有以下代码 - 这是我第一次真正尝试使用游标。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是在定义过程后调用该过程吗?如:
CALL demo();
Are you calling the procedure after defining it? as in:
CALL demo();
我不确定,但是您不应该在插入后显式提交吗?
I am not sure, but should not you explicitly commit after insert?