要找出重复数据,可以使用以下方法:
- 使用COUNT()函数和GROUP BY子句来统计每个值在表中出现的次数,然后筛选出出现次数大于1的数据。
SELECT column1, column2, COUNT(*) AS count FROM table_name GROUP BY column1, column2 HAVING count > 1;
- 使用子查询来查找重复数据,通过在WHERE子句中使用EXISTS或IN子句来筛选出重复的数据。
SELECT column1, column2 FROM table_name WHERE (column1, column2) IN ( SELECT column1, column2 FROM table_name GROUP BY column1, column2 HAVING COUNT(*) > 1 );
- 使用窗口函数ROW_NUMBER()来给每条数据编号,然后筛选出编号重复的数据。
WITH cte AS ( SELECT column1, column2, ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY column1) AS rn FROM table_name ) SELECT column1, column2 FROM cte WHERE rn > 1;