0

我创建了一个程序来从表中找出丢失的 id,但它没有检索到任何结果,下面是程序

DELIMITER ||
DROP PROCEDURE IF EXISTS proc_rpt ||
CREATE PROCEDURE proc_rpt()
BEGIN 
SET @minID = (SELECT MIN(`id`) FROM `tbl_film` WHERE `user_id`=13);
SET @maxID = (SELECT MAX(`id`) FROM `tbl_film` WHERE `user_id`=13);
REPEAT 
    SET @tableID = (SELECT `id` FROM `tbl_film` WHERE `id` = @minID);
    IF (@tableID IS NULL) THEN
        INSERT INTO temp_missing(`missing_id`) VALUES (@tableID);
    END IF;
    SET @minID = @minID + 1; 
UNTIL (@minID <= @maxID)
END REPEAT;  // earlier I was missing this line which was giving error in Procedure.
END; ||

然后我在上面打电话

CALL rpt_proc();

film_id但是在 tbl_missing 上没有结果,而我可以看到in有很多缺失值tbl_film。有人会告诉我我做错了什么吗?

4

1 回答 1

0

试试这个代码。

DELIMITER ||
    DROP PROCEDURE IF EXISTS proc_rpt ||
    CREATE PROCEDURE proc_rpt()
    BEGIN 
    DECLARE maxId int;
    DECLARE minId int;
    DECLARE curId int;
        SELECT MIN(`id`) INTO maxId FROM `tbl_film` WHERE `user_id`=13;
        SELECT MAX(`id`) INTO minId FROM  `tbl_film` WHERE `user_id`=13;
    REPEAT 
        SELECT `id` INTO curId FROM `tbl_film` WHERE `id` = @minID);
        IF (curId IS NULL) THEN
            INSERT INTO temp_missing(`missing_id`) VALUES (curId);
        END IF;
        SET minId = minId + 1; 
    UNTIL (minId <= maxId)
    END REPEAT;  // earlier I was missing this line which was giving error in Procedure.
    END; ||
于 2013-03-26T08:43:15.090 回答