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



系统偶尔会生成类似的行对,其中除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

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

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

使用子查询查找重复项:

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

相关内容

  • 没有找到相关文章

最新更新