mysql - 根据一个字段查找另一个字段重复的数据?并删除相同的记录,保留其中一个。
黄舟
黄舟 2017-04-17 16:16:10
0
3
627

根据一个字段查找另一个字段重复的数据?并删除相同的记录,保留其中一个。

数据库表: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  

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(3)
刘奇

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);

阿神

雷雷

伊谢尔伦

没有您的数据,我这里给一段代码供您参考:

删除相同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
    )
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!