分类
mysql 数据库

Mysql去重

首先查询出重复记录形成一个集合(临时表t2),集合里是每种重复记录的最小ID,然后关联判断重复基准的字段再根据条件,删除原表中id大于t2中id的记录

delete from xt_goods_attribute_copy1 a,
(
SELECT min(id) as id,three_cate_id,name FROM xt_goods_attribute GROUP BY three_cate_id,name HAVING count(*) > 1
) t2
WHERE
a.three_cate_id = t2.three_cate_id and a.name = t2.name AND a.id > t2.id;

MSSQL去重:

delete from hion_contact
where contact in (select contact from hion_contact group by contact having count() > 1)
AND (recid NOT IN (SELECT max(recid) AS recid FROM hion_contact GROUP BY khbh,contact HAVING (COUNT(contact) > 1)))

发表评论