假设有一个表A,包含两个字段id和name,表结构如下:
1 | test=# \d test; |
现在我们往表test中插入多条重复的数据:
1 | test=# insert into test values (1,'小明'); |
插入后的结果如下表:
1 | id | name |
现在我们的需求是删除表test中重复的数据。
不使用delete using的写法
常规的写法我们需要借助ROW_NUMBER函数,先根据name字段进行划分,根据id进行倒序排列,取其ROW_NUMBER值大于1的,将其删除,剩余就是我们需要的值。
1 | delete from test where id in ( |
使用delete using的写法
postgresql特有的一种写法就是使用delete using语法。
1 | delete from test a using test b where a.id<b.id and a.name=b.name; |
using 有点类似子查询,可以关联包含在where子语句中的字段的表。本例子中的a.id<b.id就是筛选出a表中id小于b表中id的记录。