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

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

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

黄舟
黄舟

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

Antworte allen(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);

阿神
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
)
伊谢尔伦

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

删除相同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
    )
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!