删除重复的行,除了一个,**其中行包含NULL值**

时间:2012-09-12 15:39:11

标签: mysql null duplicates

这个问题与涉及删除MySQL中重复行的其他SO问题类似但不同。

在一个或多个列包含NULL值的情况下,引用问题(如下)中的选择解决方案失败。我在下面的sqlfiddle中包含模式以及两个选择的答案,以说明以前的解决方案无法正常工作的位置。

来源架构:

CREATE TABLE IF NOT EXISTS `deldup` (
  `or_id` int(11) NOT NULL AUTO_INCREMENT,
  `order_id` varchar(5) NOT NULL,
  `txt_value` varchar(20) NOT NULL,
  `date_of_revision` date NOT NULL,
  `status` int(3) DEFAULT NULL,
  PRIMARY KEY (`or_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

INSERT INTO `deldup` (`or_id`, `order_id`, `txt_value`, `date_of_revision`, `status`) VALUES
(1, '10001', 'ABC', '2003-03-06', NULL),
(2, '10001', 'RFE', '2003-03-11', NULL),
(3, '10002', 'ASE', '2009-08-05', NULL),
(4, '10003', 'PEF', '2001-11-03', NULL),
(5, '10004', 'OIU', '1999-10-29', NULL),
(6, '10005', 'FOO', '2002-03-01', NULL),
(7, '10006', 'RTY', '2005-08-19', NULL),
(8, '10001', 'NND', '2003-03-20', NULL),
(9, '10005', 'VBN', '2002-02-19', NULL),
(10, '10002', 'AAQ', '2009-08-13', NULL),
(11, '10002', 'EEW', '2009-08-07', NULL),
(12, '10001', 'ABC', '2003-03-06', 3),
(13, '10001', 'ABC', '2003-03-06', 3),
(14, '10001', 'ABC', '2003-03-06', NULL),
(15, '10001', 'ABC', '2003-03-06', NULL);

解决方案示例1:

http://sqlfiddle.com/#!2/983f3/1

create temporary table tmpTable (or_id int);

insert  tmpTable
        (or_id)
select  or_id
from    deldup yt
where   exists
        (
        select  *
        from    deldup yt2
        where   yt2.txt_value = yt.txt_value

                and yt2.order_id = yt.order_id
                and yt2.date_of_revision = yt.date_of_revision
                and yt2.status = yt.status
                and yt2.or_id > yt.or_id
        );

delete  
from    deldup
where   or_id in (select or_id from tmpTable);

请注意,包含非空行值的行已成功删除,但不会删除包含空值的行(请参阅生成的SELECT查询中的第14行和第15行)。


解决方案示例2:

http://sqlfiddle.com/#!2/8a4f8/1

DELETE 
    n1 
FROM 
    deldup n1, 
    deldup n2 
WHERE 
    n1.or_id                < n2.or_id AND 
    n1.order_id             = n2.order_id AND 
    n1.txt_value            = n2.txt_value AND 
    n1.date_of_revision     = n2.date_of_revision AND
    n1.status               = n2.status 

此解决方案涉及的代码较少,与示例1的工作方式相同,包括排除包含空值的行。


如何删除其中一个列值包含NULL值的重复行?


参考文献:

Delete all Duplicate Rows except for One in MySQL?

Remove duplicate rows in MySQL

How to remove duplicate entries from a mysql db?

1 个答案:

答案 0 :(得分:3)

如何使用this

等简单调整第二个解决方案
WHERE
  ...
  (n1.status IS NULL AND n2.status IS NULL 
   OR n1.status = n2.status)

我假设您只在重复所有值时才认为记录是重复的。