CREATE TABLE IF NOT EXISTS `result` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(150) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`lead_id`),
UNIQUE KEY `lead_id` (`lead_id`),
KEY `email` (`email`),
KEY `active` (`active`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
INSERT INTO result (email, active) VALUES
('aaa@xxx.com', 1),
('aaa@xxx.com', 0),
('aaa@xxx.com', 0),
('aaa@xxx.com', 1),
('bbb@xxx.com', 1),
('bbb@xxx.com', 1),
('bbb@xxx.com', 0);
lead_id | email | active
---------------------------
1 | aaa@xxx.com | 1
2 | aaa@xxx.com | 0
3 | aaa@xxx.com | 0
4 | aaa@xxx.com | 1
5 | bbb@xxx.com | 1
6 | bbb@xxx.com | 1
7 | bbb@xxx.com | 0
我需要更新表格,以便只有一行或任何一组匹配的电子邮件地址处于活动状态
我正在努力编写一个查询来执行此操作。哪一行无关紧要。结果看起来像这样
lead_id | email | active
---------------------------
1 | aaa@xxx.com | 1
2 | aaa@xxx.com | 0
3 | aaa@xxx.com | 0
4 | aaa@xxx.com | 0
5 | bbb@xxx.com | 1
6 | bbb@xxx.com | 0
7 | bbb@xxx.com | 0
感谢您的建议
答案 0 :(得分:2)
以下方法为active
/ 0
组设置lead_id
至email
,该组没有最小lead_id
值并且也处于活动状态。对于GROUP BY
lead_id
和1
的{{1}}值,5
子查询将使用您的示例数据创建两条记录。然后将result
表连接到此表,并且不与子查询匹配的任何记录都将其active
值设置为0
。这没关系,因为该值已经是0
,或者它是我们希望删除的副本。
UPDATE result AS r1
LEFT JOIN
(
SELECT MIN(lead_id) AS min_lead_id, email, active
FROM result
WHERE active = 1
GROUP BY email, active
) AS r2
ON r1.lead_id = r2.min_lead_id AND r2.email = r2.email
SET r1.active = 0
WHERE r2.active IS NULL