从表中删除值,该值依赖于另一行

时间:2014-03-12 18:25:40

标签: mysql datetime sql-delete

我在表格中有大约20个Lakhs行数据,如下所示

msisdn  -      Date                    - time         Remarks
981109  - 24 -12-2013  23:12:12  not to be deleted
981109  - 24 -12-2013  23:14:12  to be Deleted as time diff is less then 9 minute 
981109  - 24 -12-2013  23:16:12  to be Deleted as time diff is less then 9 minute 
981110  - 24 -12-2013  23:26:10  not to be deleted as msisdn got changed
981110  - 25 -12-2013  12:24:12  not to be deleted as date is same but time diff is >9minute 
981110  - 25 -12-2013  12:34:12  not to be deleted as date is same but time diff is >9minute 
981110  - 25 -12-2013  12:43:12  Delete as the time diff is less then 9 mnte and msisdn &  &date are same
981110  - 25 -12-2013  13:45:12  Delete as the time diff is less than 9 mnte and last row msisdn and date are same.

这里我只想删除两行(或x行)之间的时差小于9分钟的那些行,但条件是这些行的msisdn应该相同,日期应该相同,时间间隔应该是不到9分钟。

1 个答案:

答案 0 :(得分:0)

也许你可以尝试一个程序:

delimiter $$
DROP PROCEDURE IF EXISTS p_delete_rows$$

CREATE PROCEDURE p_delete_rows(
  OUT returnflag INT(1)
)
BEGIN

    DECLARE EXIT HANDLER FOR SQLEXCEPTION 
    BEGIN 
        /* RETURN -1 IF PROCEDURE FAIL */
        SET returnflag = -1;
    END;

    SET AUTOCOMMIT = 0;
    SET returnflag = 0;

    BEGIN
        DECLARE previous_msisdn INT DEFAULT 0;
        DECLARE row_msisdn INT;

        DECLARE previous_date DATETIME DEFAULT '1901-01-01 00:00:00';
        DECLARE row_date DATETIME;  

        DECLARE myCursor cursor for SELECT msisdn, tdate FROM mytable;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneLoop = TRUE;

        OPEN myCursor;  
        read_myloop: LOOP

            SET doneLoop = FALSE ;
            FETCH myCursor into row_msisdn, row_date;
            IF doneLoop THEN
                LEAVE read_myloop;
            END IF;

            /* CHECK IF INTERVAL IS LESS THAN 9 MINUTE */
            IF row_msisdn = previous_msisdn 
                AND row_date < DATE_ADD(previous_date, interval 9 MINUTE)
            THEN
                DELETE FROM mytable where msisdn = row_msisdn AND tdate = row_date;
            END IF:
             /* SET PREVIOUS ROW VALUES */
            SET previous_msisdn = row_msisdn;
            SET previous_date = row_date;

        END LOOP;
        CLOSE myCursor;                     

    END;

    SET returnflag = 1;

    COMMIT;
    SET AUTOCOMMIT = 1;

END$$

delimiter ;

dans称之为:

CALL p_delete_rows(@flag);