SQL 根据两列删除重复记录
SQL Delete duplicate records based on two columns
在 postgresql 中
我需要查找 table 以便在两列中具有唯一值。我有这份汽车清单,但高尔夫在汽车和商店列中重复。我想删除所有汽车和商店相同的记录(因为马力和公里数将相同)
所以输出 table 应该是:
谢谢!!
您可以使用 distinct on
:
select distinct on (car, shop) t.*
from t
order by car, shop, day;
如果要真正删除记录:
delete from t
where t.day = (select min(t2.day)
from t2
where t2.car = t.car and t2.shop = t.shop
);
在 postgresql 中
我需要查找 table 以便在两列中具有唯一值。我有这份汽车清单,但高尔夫在汽车和商店列中重复。我想删除所有汽车和商店相同的记录(因为马力和公里数将相同)
所以输出 table 应该是:
谢谢!!
您可以使用 distinct on
:
select distinct on (car, shop) t.*
from t
order by car, shop, day;
如果要真正删除记录:
delete from t
where t.day = (select min(t2.day)
from t2
where t2.car = t.car and t2.shop = t.shop
);