我使用它从 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/