How to use the physical location of rows (ROWID) in a DELETE statement

前端 未结 3 1807
滥情空心
滥情空心 2021-01-06 06:25

I have a table that has a lot of duplicated rows and no primary key.
I want to remove just the duplicated records, but when I try to do this it would remove all peers.

3条回答
  •  走了就别回头了
    2021-01-06 06:54

    Simplify this by one query level:

    DELETE FROM table_name
    WHERE  ctid NOT IN (
       SELECT min(ctid)
       FROM   table_name
       GROUP  BY $other_columns);
    

    .. where duplicates are defined by equality in $other_columns.
    There is no need to include columns from the GROUP BY clause in the SELECT list, so you don't need another subquery.

    ctid in the current manual.

提交回复
热议问题