赞
踩
通过使用GROUP BY子句对需要检查重复记录的字段进行分组,并使用HAVING子句筛选出出现次数大于1的分组。
- SELECT column1, column2, ...
- FROM table
- GROUP BY column1, column2, ...
- HAVING COUNT(*) > 1;
通过使用子查询,检查表中是否存在与当前记录具有相同值的其他记录。
- SELECT column1, column2, ...
- FROM table t1
- WHERE EXISTS (
- SELECT 1
- FROM table t2
- WHERE t1.column1 = t2.column1
- AND t1.column2 = t2.column2
- ...
- AND t1.id <> t2.id
- );
使用窗口函数根据需要检查的字段进行分区,并计算每个分区中的记录数。然后使用CTE过滤出记录数大于1的分区
- WITH cte AS (
- SELECT column1, column2, ..., COUNT(*) OVER (PARTITION BY column1, column2, ...) AS count
- FROM table
- )
- SELECT column1, column2, ...
- FROM cte
- WHERE count > 1;
使用ROW_NUMBER函数为表中的每个记录分配一个行号,并筛选出行号大于1的记录。
- WITH cte AS (
- SELECT column1, column2, ..., ROW_NUMBER() OVER (PARTITION BY column1, column2, ... ORDER BY id) AS row_num
- FROM table
- )
- SELECT column1, column2, ...
- FROM cte
- WHERE row_num > 1;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。