简体   繁体   English

使用MinID和Complex Select从MYsql表中删除重复项

[英]Remove Duplicates from MYsql table using MinID and Complex Select

I found this here to delete records with min ID: 我在这里找到这是为了删除具有最小ID的记录:

DELETE FROM Table WHERE id NOT IN (SELECT MIN(id) FROM Table GROUP BY FieldA)

However I don't want all the found dupes in the table to have the one with the lower ID removed, only a subset of them. 但是,我不希望表中所有找到的重复对象都具有较低ID的重复对象被删除,而只是其中的一部分。 I have other criteria for other dupes patterns. 对于其他伪造图案,我还有其他标准。 So I made my select to get the subset of records that have dupes AND other conditions where I then DO want the min ids removed: 因此,我做出选择以获取具有重复和其他条件的记录的子集,然后我希望删除最小标识:

Select min(Z),Max(Z),count(*) from Table  
group by P,N
having count(*)>1 and Min(Z)!=Max(Z) and Min(Z)>0 

I am unclear how to first get that subset of records and THEN remove the minID from the dupes in that subset 我不清楚如何首先获取该记录的子集,然后从该子集的重复对象中删除minID

In MySQL use LEFT JOIN : 在MySQL中,使用LEFT JOIN

delete t
    from table t left join
         (Select min(Z), Max(Z), count(*) 
          from Table  
          group by P, N
          having count(*) > 1 and Min(Z) <> Max(Z) and Min(Z) > 0
         ) tt
         on t.? = tt.?
    where tt.? is null; 

It is unclear how the id is defined in your expression. 目前尚不清楚在表达式中如何定义id It is also unclear whether the subquery generates the ids to keep or to delete. 还不清楚子查询是否生成要保留或删除的ID。 The version above assumes it is generating the ids to keep. 上面的版本假定它正在生成要保留的ID。 (If it generates the ids to delete, then use inner join and get rid of the where clause.) (如果它生成要删除的ID,则使用inner join并摆脱where子句。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM