天天看點

sql 删除一張表中重複的資料

delete from dc_entry_detail where  id in(

      select id from dc_entry_detail  a,(select source_id from  dc_entry_detail group by source_id having count(*) >1) b

      where a.source_id = b.source_id  

      and a.id in (select min(id) from dc_entry_detail where a.source_id = source_id)

      ) 

詳解:delete 表A  where id in (select id from 表A a,(select 表中等值字段c from 表A group by 表中等值字段 having count (*)>1) b

where a.表中等值字段=b.表中等值字段

and a.表中等值字段c=b.表中等值字段c

and a.id in(select min(id) from 表A where a.表中等值字段c=表中等值字段c)

)

 select  T1.CopGNo,T1.HsCode,T1.CName,T1.EName,T1.CreateDate 

  FROM  myhg_cop_material AS T1

   JOIN  

  (SELECT   CopGNo,MAX(CreateDate) CreateDate    FROM  myhg_cop_material

WHERE Column5 ='0' AND  CreateDate >'2014-01-01' and CreateDate <='2020-04-01' 

 GROUP  BY CopGNo) AS T2 ON T1.CopGNo=T2.CopGNo AND T1.CreateDate=T2.CreateDate

 WHERE T1.Column5 ='0' AND  T1.CreateDate >'2014-01-01' and T1.CreateDate <='2020-04-01'