根据一个字段查找另一个字段重复的数据?并删除相同的记录,保留其中一个。
数据库表:product_code_relate_titletext
字段:id,search_id,product_code,raw_title
需求:根据product_code重找raw_title重复的数据?(已实现)
需求:根据product_code重找raw_title重复的数据,并且删除相同的数据,保留id最少的记录即可。
(注意:删除的是product_code下raw_title重复的数据)
select * from product_code_relate_titletext p where (select count(1) from product_code_relate_titletext where product_code=p.product_code and raw_title=p.raw_title)>1
怪我咯2017-04-17 16:18:10
delete from product_code_relate_titletext where id not in (select * from (select min(id) from product_code_relate_titletext group by product_code,raw_title having count(*) > 1) as b);
阿神2017-04-17 16:18:10
delete from product_code_relate_titletext where id in
(
select a.id from
(select * from product_code_relate_titletext where id not in (select min(id) from product_code_relate_titletext group by product_code,raw_title)
) a
)
伊谢尔伦2017-04-17 16:18:10
I don’t have your data, so I’ll give you a piece of code for your reference:
删除相同Name除ID最小的记录外的其他记录
-- ----------------------------
-- Table structure for t_team
-- ----------------------------
DROP TABLE IF EXISTS `t_team`;
CREATE TABLE `t_team` (
`id` int(11) NOT NULL,
`user_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_team
-- ----------------------------
INSERT INTO `t_team` VALUES ('1', 'A');
INSERT INTO `t_team` VALUES ('2', 'B');
INSERT INTO `t_team` VALUES ('3', 'B');
INSERT INTO `t_team` VALUES ('4', 'A');
INSERT INTO `t_team` VALUES ('5', 'C');
INSERT INTO `t_team` VALUES ('6', 'C');
-- ----------------------------
-- 删除NAME重复的记录,保留ID最小的记录
-- ----------------------------
DELETE FROM t_team WHERE id NOT IN (
SELECT min_id FROM (
SELECT MIN(id) AS min_id FROM t_team GROUP BY user_name
) B
)