PostgreSQL 删除重复行,只保留一行

delete from table_name as ta where ta.唯一键 <> ( select max(tb.唯一键) from table_name as tb where ta.判断重复的列 = tb.判断重复的列);

例子

delete from wr_yr_sync_log as wylog where wylog.log_id <> (select max(wylog2.log_id) from wr_yr_sync_log as wylog2 where wylog.yr_cuid = wylog2.yr_cuid);

如果station_id为uuid类型,查询出重复数据:

select count(*) from wr_space_station as ss where concat(ss.station_id,'') <> (select min(concat(ss2.station_id,'')) from wr_space_station as ss2 where ss.station_name = ss2.station_name);

如果station_id为uuid类型,删除重复数据:

delete from wr_space_station as ss where concat(ss.station_id,'') <> (select min(concat(ss2.station_id,'')) from wr_space_station as ss2 where ss.station_name = ss2.station_name);

 

你可能感兴趣的:(数据库,PostgreSQL删除重复行,PostgreSQL只保留一行,PGSQL删除重复行只保留一行)