SQL 所有数据只保留一条,其他重复数据删除

爬的免费代理ip,去重

CREATE TABLE `iproxy` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `ip` varchar(15) DEFAULT NULL,
  `port` varchar(11) DEFAULT '',
  `protocol` varchar(5) DEFAULT 'http',
  `status` char(1) DEFAULT 'Y',
  `bySite` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15427 DEFAULT CHARSET=utf8;

查询所有数据(保留一条),其他重复数据

SELECT * from iproxy where (ip,port) in (select ip,port from iproxy group by ip,port having count(*) > 1) and id not in (select min(id) from iproxy group by ip,port having count(*)>1)

所有数据只保留一条,其他重复数据删除

DELETE from iproxy where (ip,port) in (select ip,port from iproxy group by ip,port having count(*) > 1) and id not in (select min(id) from iproxy group by ip,port having count(*)>1)

- THE END -
版权声明:
转载原创文章请注明,文章出处://kinggoo.com
原文地址:https://kinggoo.com/sql-only-one.htm
发表评论?

0 条评论。

发表评论


此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据