mysql - 选择要删除哪个重复行?

标签 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.*”,它不会显示要删除的行,而是显示表中的所有行?

最佳答案

为了获取要删除的值,您可以使用并与inspection_text的值witg max_len进行内部连接 对于重复的行并删除长度 <> 到 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 

关于mysql - 选择要删除哪个重复行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45803770/

相关文章:

sql - 如何将 Dapper 与 MS SQL Server (2012) 地理空间/SQLGeography 列结合使用

mysql - 使用 MySQL 而不是 MariaDB 的 XAMPP?

php - 基于两列的排名

mysql - 更新 mysql/mariadb 表时发送电子邮件的干净、低开销的方式?

mysql - 在 mysql select 语句中使用每 2 个字符后在字符串中添加连字符

php - 使用 PHP 和网站跟踪 iPhone 应用程序的使用情况

mysql - 这个查询可以在没有 ONION 的情况下重写吗?它是否可扩展?

mysql - 错误 1064 (42000) - INSERT ... SELECT 查询中的 MySQL 错误

php - 在php View 中组合MYSQL查询

mysql - 在某种条件下找到每件元素和最昂贵的元素