1

系统偶尔会生成相似的行对,其中所有列都相同,除了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 值。

4

2 回答 2

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
于 2012-11-12T15:32:54.057 回答
-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
于 2012-11-12T15:19:20.087 回答