我有一个触发器,可以构成临时表格。现在我想遍历临时表并在每一行上做一些事情。有一些简单的方法吗?我用Google搜索了,但是对于这么简单的任务来说,一切似乎都过于复杂。
CREATE TRIGGER myTrigger AFTER UPDATE ON myTable
FOR EACH ROW
BEGIN
IF NEW.col <> OLD.col THEN
DROP TEMPORARY TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE IF NOT EXISTS tmpTable AS (my select statement);
--For each row in tempTable--
Do something
--
DROP TEMPORARY TABLE tmpTable;
END IF;
END
答案 0 :(得分:3)
拥有rownumber你可以这样做:
DROP TEMPORARY TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE IF NOT EXISTS tmpTable AS (
SELECT l.*,
@curRow := @curRow + 1 AS row_no
FROM XXX_TABLE l
JOIN (SELECT @curRow := 0) r);
使用它您可以使用WHILE
循环:
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM tmpTable INTO n;
SET i=0;
WHILE i<n DO
SET i = i + 1;
-- do sth WHERE row_no=i;
END WHILE;