我试图从ID重复的表中删除数据但是将另一列保留在某个值
key . id . info . flag
1 . A . apple . y
2 . B . banana . n
3 . B . banana . y
4 . C . orange . n
我想删除第2行" id"是重复的并保持标志值为" y"。该表是> 100K行
我尝试过DISTINCT和GROUP BY的各种组合而没有很好的结果
答案 0 :(得分:1)
您可以使用group by,count和having来过滤可能的id 如果你想用y
删除值 delete from
my_table where id in (
select t.id from(
select id
from my_table
group by id
having count(*) > 1
) t
and flag ='y')
否则如果你想保留y那么
delete from
my_table where id in (
select t.id from(
select id
from my_table
group by id
having count(*) > 1
) t
and flag <>'y')