想要逐行遍历一个表,如果另一个表中有匹配的行,请删除限制为1

时间:2019-02-21 19:38:38

标签: sql mysql-5.6

我实质上是在尝试进行手动设置减法(在不消除重复项的情况下,如果重复项存在,我需要保留它们)。我有一个要逐行进行迭代的表,如果在更大的表中存在匹配项,请使用存储过程在更大的表中删除匹配项的单个实例。我该怎么办?我有三个条件,并写有以下内容-

DROP PROCEDURE deleteOverlappingBottles;
DELIMITER ;;

CREATE PROCEDURE deleteOverlappingBottles()
BEGIN
DECLARE n INT default 0;
DECLARE i INT default 0;
SELECT 10 FROM allBottles INTO n;
SET i = 0;
WHILE i<n DO
    #
    What goes here?  How can I get the current row of allBottles and specifically the values needed for the proceeding where clause?
    DELETE FROM historicdata2Delete as t
    WHERE t.unitADcode = ab.bottleBarcode AND t.bottle_timestamp = ab.t_stamp
    AND t.prepackId = ab.parentPrepackId LIMIT 1;
    SET i = i+1;
END WHILE;
End;
;;
DELIMITER ;

CALL deleteOverlappingBottles();

使用MySQL 5.6。

historicDataToDelete
unitADcode            bottle_timestamp        prepackId
barcode1              timestamp1              id1
barcode1              timestamp1              id1
barcode2              timestamp2              id2
barcode3              timestamp3              id3
barcode3              timestamp3              id3

allBottles
bottleBarcode         t_stamp                 parentPrepackId
barcode1              t_stamp1                id1
barcode1              t_stamp1                id1
barcode3              t_stamp3                id3

因此,通过allBottle逐行进行,并在每个实例中删除一行,都会使HistoricData2Delete保持不变

historicDataToDelete
unitADcode            bottle_timestamp        prepackId
barcode2              timestamp2              id2
barcode3              timestamp3              id3

到目前为止,这是我目前所能使用的,但是它没有用,关于为什么的任何想法?

DELIMITER ;;

CREATE PROCEDURE deleteOverlappingBottles()
BEGIN
DECLARE n INT default 0;
DECLARE i INT default 0;

SELECT 10 FROM allBottles INTO n;
SET i = 0;
WHILE i<n DO
    SET @barcode = (SELECT bottleBarcode FROM allBottles LIMIT i,1);
    SET @tstamp = (SELECT t_stamp FROM allBottles LIMIT i, 1);
    SET @preIdx = (SELECT parentPrepackId FROM allBottles LIMIT i, 1);
    DELETE FROM historicdata2Delete
    WHERE unitADcode = @barcode AND bottle_timestamp = @tstamp AND prepackIdx = @preIdx LIMIT 1;
    SET i = i+1;
END WHILE;
End;
;;
DELIMITER ;

CALL deleteOverlappingBottles();

它说结果包括多于一行。它确实运行了一段时间,一个多小时后我将其停止,但没有删除任何内容。

1 个答案:

答案 0 :(得分:1)

似乎您只想念cursor

参考代码可在此处找到:MySql Cursors

CREATE PROCEDURE deleteOverlappingBottles()
BEGIN
DECLARE done INT DEFAULT FALSE;
-- Variables for FETCH (change data types to your needs)
DECLARE vBottleBarcode CHAR(100);
DECLARE vt_stamp TIMESTAMP;
DECLARE vParentPrepackId INT;
-- cursor to iterate through the table
DECLARE cur1 CURSOR FOR SELECT BottleBarcode, t_stamp, ParentPrepackId FROM allBottles;
-- apparently MySql needs this to handle the end of the table
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur1;

read_loop: LOOP
    -- get the three column values for the current row
    FETCH cur1 INTO vBottleBarcode, vt_stamp, vParentPrepackId;
    IF done THEN
      LEAVE read_loop;
    END IF;
    -- use the variables from FETCH to identify the row in historicdataToDelete
    DELETE FROM historicdata2Delete as t
    WHERE t.unitADcode = vBottleBarcode AND t.bottle_timestamp = vt_stamp
    AND t.prepackId = vParentPrepackId
    LIMIT 1; -- delete only 1 row
  END LOOP;

  CLOSE cur1;
END LOOP;
End;

免责声明:我不熟悉MySql。语法可能略有错误,请适应您的需求。