我的表中有需要根据几个重复列删除的行。
例如Col1,Col2,Col3,Col4
如果Col1、Col2和Col3是重复的,那么不管Col4中的值是多少,我都希望删除这两个重复项。我该怎么做?
发布于 2016-03-18 03:39:16
您可以使用where
子句来完成这一任务:
delete from t
where (col1, col2, col3) in (select col1, col2, col3
from t
group by col1, col2, col3
having count(*) > 1
);
发布于 2016-03-18 03:40:18
按这些ID分组,并检查是否有重复的。使用所找到的副本,删除记录。
delete from mytable
where (col1,col2,col3) in
(
select col1,col2,col3
from mytable
group by col1,col2,col3
having count(*) > 1
);
发布于 2016-03-18 03:44:07
如果另一个具有相同col1、col2和col3的行以较低的col4值存在,则使用col4删除行。即保留一个col1,col2,col3行。
delete from tablename t1
where exists (select 1 from tablename t2
where t2.col1 = t1.col1
and t2.col2 = t1.col2
and t2.col3 = t1.col3
and t2.col4 < t1.col4)
要删除这两行/所有行,跳过col4条件,改为执行group by
:
delete from tablename t1
where exists (select 1 from tablename t2
where t2.col1 = t1.col1
and t2.col2 = t1.col2
and t2.col3 = t1.col3
group by t2.col1, t2.col2, t2.col3
having count(*) > 1)
https://stackoverflow.com/questions/36083368
复制相似问题