How to delete duplicate records in mysql table

Aston hair

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL COMMENT 'name',
  `age` tinyint(4) DEFAULT NULL COMMENT 'age',
  `mate` tinyint(4) DEFAULT '1' COMMENT 'With or without a spouse (1-yes 0-no)',
  PRIMARY KEY (`id`),
  KEY `idx_name` (`name`),
  KEY `idx_age` (`age`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

 Existing records:

insert  into `test`(`id`,`name`,`age`,`mate`) values (2,'aaaaa',28,0),
(3,'bbbb',23,0),
(4,'cccc',25,1),
(5,'dddd',26,0),
(6, 'eeee', 24.0),
(7,'fffff',18,0),
(8, 'eeee', 40.1),
(9, 'eeee', 60.1);

 If you want to remove the record with the same name, the method is as follows

alter ignore table test add unique idx_name (name);

 It will delete duplicate records (don't be afraid, it will keep one), and then build a unique index, which is efficient and user-friendly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394879&siteId=291194637