从其他重复的行对中删除null - mysql

时间:2012-11-12 15:14:31

标签: mysql optimization select null

系统偶尔生成类似的行对,除status_code外,所有列都相同。不相似的列对包含NULL和数字。

CREATE TABLE IF NOT EXISTS `delnull` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `process_id` varchar(16) DEFAULT NULL,
  `somedate` datetime DEFAULT NULL,
  `status_desc` varchar(200) DEFAULT NULL,
  `status_code` int(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Dumping data for table `delnull`
--

INSERT INTO `delnull` (`id`, `process_id`, `somedate`, `status_desc`, `status_code`) VALUES
(1, 'xyz', '2012-11-12 10:01:43', 'Completed OK', 0),
(2, 'xyz', '2012-11-12 10:01:43', 'Completed OK', NULL),
(3, 'def', '2012-11-13 10:02:11', 'Failed: broken connection', 3),
(4, 'ghi', '2012-11-09 10:02:23', 'Lost packets', 4),
(5, 'ghi', '2012-11-01 10:04:30', 'Failed: broken connection', 3),
(6, 'ghi', '2012-11-06 10:04:23', 'Lost packets', 4),
(7, 'pos', '2012-11-02 10:06:01', 'Completed OK', 0),
(8, 'pos', '2012-11-02 10:06:02', 'Completed OK', NULL);

http://sqlfiddle.com/#!2/128cc/1/0

在status_code中识别包含NULL值的行的最佳方法是什么? (在数据集中,所讨论的行对是#1和#2,我需要识别#2,因为它是该对的NULL值。

2 个答案:

答案 0 :(得分:1)

DELETE FROM delnull NATURAL JOIN (

  -- find all those groups that have both a NULL and a non-NULL record
  SELECT   process_id, somedate, status_desc
  FROM     delnull
  GROUP BY process_id, somedate, status_desc
  HAVING   SUM(status_code IS NOT NULL) AND SUM(status_code IS NULL)

) t WHERE delnull.status_code IS NULL

答案 1 :(得分:-1)

使用子查询可以查找重复项:

delete from `delnull` dn1
where dn1.status_code is null
and (
  select count(*) 
  from `delnull` dn2
  where dn2.id = dn1.id
  and dn2.process_id = dn1.process_id
  and dn2.somedate = dn1.somedate
  and dn2.status_code = dn1.status_code
   ) = 2