Mysql数据表去重

Posted 23云恋49枫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql数据表去重相关的知识,希望对你有一定的参考价值。

查询不重复元素个数

select count(distinct domain) from black_botnet_domian; 

查询表中元素个数大于等于2的元素

SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING COUNT(goods_name)>=2;  

然后使用LEFT JOIN使原始表和上述查询结果进行连接,删除重复记录,保留id较小的记录

DELETE t1 FROM tdb_goods AS t1 LEFT JOIN( SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING COUNT(goods_name)>=2) AS t2 ON t1.goods_name = t2.goods_name WHERE t1.goods_id>t2.goods_id; 

如果想保留id较大的同理,如下所示

DELETE t1 FROM tdb_goods AS t1 LEFT JOIN( SELECT max(goods_id) AS goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING COUNT(goods_name)>=2) AS t2 ON t1.goods_name = t2.goods_name WHERE t1.goods_id<t2.goods_id;

以上是关于Mysql数据表去重的主要内容,如果未能解决你的问题,请参考以下文章

linux中怎么查看mysql数据库版本

mysql多表查询去重

mysql的去重问题

部分代码片段

求mysql 语句去重并按重复个数排序

MySQL数据去重