选择要删除的重复行?

时间:2017-08-21 18:45:46

标签: mysql sql mariadb

我正在使用它来删除MySql / MariaDB表中基于三列相同的重复记录。

DELETE a FROM stest as a, stest as b
WHERE
      (a.facility_id=b.facility_id OR a.facility_id IS NULL AND b.facility_id IS NULL)
  AND (a.inspection_date=b.inspection_date OR a.inspection_date IS NULL AND b.inspection_date IS NULL)
  AND (a.deficiency_tag=b.deficiency_tag OR a.deficiency_tag IS NULL AND b.deficiency_tag IS NULL)
  AND a.recno < b.recno;

我想做的是,在有重复记录的地方,保留一个长度最大(inspection_text)列的记录。 (很可能,inspection_text列将是相同的,但如果它们不相同,我想删除较小的列)

有人可以告诉我如何修改上述语句以添加此条件吗?

我也很好奇DELETE是如何工作的,但是如果我将“DELETE a”更改为“SELECT a。*”它没有显示要删除的行但是表中的所有行?

1 个答案:

答案 0 :(得分:1)

用于获取要删除的值,可以使用和内部连接,其值为wit_ max_len of inspection_text 对于dupliceted行并删除lenght&lt;&gt;行到max_len

   delete from  stest 
   inner join ( 

       select  facility_id, deficiency_tag, inspection_date , max(length( inspection_text)) as  max_len from stest
       where ( facility_id, deficiency_tag, inspection_date ) in ( 

       select facility_id, deficiency_tag, inspection_date 
       from stest
       group by facility_id, deficiency_tag, inspection_date
       having count(*) > 1 
       ) 
       group by  facility_id, deficiency_tag, inspection_date
   ) t  on stest.facility_id = t.facility_id 
              and stest.deficiency_tag = t.deficiency_tag
                and stest.inspection_date = t.inspection_date
                  and length( stest.inspection_text) <> t.max_len 

这不会使用元组进行连接

   delete from  stest 
   inner join ( 
       select  
            facility_id
          , deficiency_tag
          , inspection_date 
          , max( length( inspection_text) ) as  max_len 
       from stest
       innert join ( 
          select 
              facility_id
            , deficiency_tag
            , inspection_date 
         from stest
         group by facility_id, deficiency_tag, inspection_date
         having count(*) > 1 
       ) t2 on stest.facility_id = t2.facility_id and stest.deficiency_tag = t2.deficiency_tag and stest.inspection_date = t2.inspection_date
       group by  facility_id, deficiency_tag, inspection_date
   ) t  on stest.facility_id = t.facility_id 
              and stest.deficiency_tag = t.deficiency_tag
                and stest.inspection_date = t.inspection_date

相同版本没有内连接,但其中..

   delete from  stest ,  ( 
       select  
            facility_id
          , deficiency_tag
          , inspection_date 
          , max( length( inspection_text) ) as  my_max_len 
       from stest, ( 
          select 
              facility_id
            , deficiency_tag
            , inspection_date 
         from stest
         group by facility_id, deficiency_tag, inspection_date
         having count(*) > 1 
       ) t2 where  stest.facility_id = t2.facility_id and stest.deficiency_tag = t2.deficiency_tag and stest.inspection_date = t2.inspection_date
       group by  facility_id, deficiency_tag, inspection_date
   ) t  where  stest.facility_id = t.facility_id 
              and stest.deficiency_tag = t.deficiency_tag
                and stest.inspection_date = t.inspection_date
                    and length( stest.inspection_text) <> t.my_max_len