我想删除表中的重复数据,我知道有一种使用方法
SELECT
fruit,
COUNT( fruit )
FROM
basket
GROUP BY
fruit
HAVING
COUNT( fruit )> 1
ORDER BY
fruit;
要找到它们,购买,我需要确定每一列的值是否相等,这意味着tableA.* = tableA.*
(id
除外,id是自动递增主键)
我尝试过:
SELECT
*,
COUNT( * )
FROM
myTable
GROUP BY
*
HAVING
COUNT( * )> 1
ORDER BY
id;
但是它说我不能使用GROUP BY *
,那么如何查找和删除重复的数据(需要除id以外的每一列值都相等)?
答案 0 :(得分:0)
使用
SELECT * DISTINCT
DISTINCT
删除重复的结果
答案 1 :(得分:0)
您需要尝试类似以下操作。您将PARTITION BY
应用于ID以外的其他列(因为它正在增加唯一值)。 PARTITION BY
应该应用于您要检查重复项的列。
另请参阅Row_Number in Postgres和Common Table expression in Postgres
WITH DuplicateTableRows AS
(
SELECT Id, Row_Number() OVER (PARTITION BY col1, col2... ORDER BY Id)
FROM
Table1
)
DELETE FROM Table1
WHERE Id IN (SELECT Id FROM Table1 WHERE row_number > 1)
答案 2 :(得分:0)
您可以使用JSON执行此操作:
select (to_jsonb(b) - 'id')
from basket b
group by 1
having count(*) > 1;
结果为JSON。不幸的是,要将值提取回记录中,您需要单独列出各列。