构造测试数据
CREATE TABLE basket(
id SERIAL PRIMARY KEY,
fruit VARCHAR(50) NOT NULL
);
INSERT INTO basket(fruit) values('apple');
INSERT INTO basket(fruit) values('apple');
INSERT INTO basket(fruit) values('orange');
INSERT INTO basket(fruit) values('orange');
INSERT INTO basket(fruit) values('orange');
INSERT INTO basket(fruit) values('banana');
去重
- 方法1:使用 DELETE USING 语句删除重复的行
DELETE
FROM
basket a
USING basket b
WHERE
a.id < b.id
AND a.fruit = b.fruit;
- 方法2:使用子查询删除重复的行
DELETE FROM basket
WHERE id IN
(SELECT id
FROM
(SELECT id,
ROW_NUMBER() OVER( PARTITION BY fruit
ORDER BY id ) AS row_num
FROM basket ) t
WHERE t.row_num > 1 );
- 方法3: 使用ctid删除
delete from basket where ctid not in (select min(ctid) from basket group by id);
- 方法4: 使用ctid删除2
delete from basket a where a.ctid<>(select min(ctid) from basket b where a.fruit=b.fruit);
视图去重
如果是对于视图进行去重,由于视图没有ctid,所以无法通过ctid去重;
可以使用方法1与2;