删除关系表中的重复行

时间:2019-12-04 15:28:39

标签: sql postgresql

我在form: { comment: '', }, replyForm: { comment: '', }, baseUrl: '/comments', customUpdateUrl: '/comments/update', 数据库中有一个关系表。 我要删除重复的行。

我的桌子看起来像这样:

PostgreSQL

我想要这个:

idUser      idFunction
1           1
2           1
3           1
3           1
4           1
4           1
4           2

我已经尝试过了:

idUser      idFunction
1           1
2           1
3           1
4           1
4           2

但是DELETE TOP (SELECT COUNT(*) -1 FROM user_function WHERE idUser IN (SELECT idUser FROM user_function GROUP BY idUser, idFunction HAVING COUNT(*) > 1) AND idFunction IN (SELECT idFunction FROM user_function GROUP BY idUser, idFunction HAVING COUNT(*) > 1)) FROM user_function WHERE idUser IN (SELECT idUser FROM user_function GROUP BY idUser, idFunction HAVING COUNT(*) > 1) AND idFunction IN (SELECT idFunction FROM user_function GROUP BY idUser, idFunction HAVING COUNT(*) > 1) 告诉我'TOP'处有PostgreSQL

如何删除error中关系表中的重复行?

1 个答案:

答案 0 :(得分:0)

我建议您重新创建表:

create table temp_user_function as
    select distinct idUser, idFunction
    from user_function;

truncate table user_function;   -- backup first!!!

insert into user_function (idUser, idFunction)
    select idUser, idFunction
    from temp_user_function;

修复数据后,应声明该对为唯一。