【发布时间】:2019-01-21 12:14:46
【问题描述】:
我的procedure 有问题。我有表oferty_in,其中包含字段(id、status、...、id_om)。我想要用相同的id_om 检查if exist 行的程序。
如果存在,删除行where status = 'N'(N - 新)。
我的程序几乎可以正常工作,但我在循环中迭代时遇到问题。每次我运行我的程序时,程序都会删除一半的行。不知道哪里出了问题……
DELIMITER //
CREATE PROCEDURE check_duplicates_oferty_in()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE v_id_del BIGINT;
SELECT count(*) INTO n FROM oferty_in where status_oferty = 'N';
SET i=0;
WHILE i<n DO
IF EXISTS (SELECT id_om FROM oferty_in group by id_om having count(*) >= 2 LIMIT i,1) THEN
SELECT id_om INTO v_id_del FROM oferty_in group by id_om having count(*) >= 2 LIMIT i,1;
DELETE from oferty_in where id_om = v_id_del and status_oferty = 'N';
END IF;
SET i=i+1;
END WHILE;
END
//
我也试试:
IF EXISTS (SELECT id_om FROM oferty_in group by id_om having count(*) >= 2 LIMIT i,1) THEN
SELECT id_om INTO v_id_del FROM oferty_in group by id_om having count(*) >= 2 LIMIT i,1;
DELETE from oferty_in where id_om = v_id_del and status_oferty = 'N';
SET i=i+1;
ELSE
SET i=i+1;
END IF;
但它是一样的。
每次半行。当status = 'N' 时,我使用计数器'i' 和while loop 逐行迭代oferty_in 中的行。有人知道我做错了什么吗?感谢您的帮助和时间。
【问题讨论】:
标签: mysql sql variables procedure