gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:25:57 25 4
gpt4 key购买 nike

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

最佳答案

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

关于mysql - 从其他重复行对中删除 null - mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13346277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com