前言
因为之前数据库的字段添加了索引,但是没有添加唯一索引约束,导致数据库的日月年数据出现的重复。
查询
1
| select distinct * from table 表名 where (条件)
|
1
| select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
|
可以利用row_number()建立一个值区分去重
1 2 3
| select * from (select *, row_number() over(partition by 分组字段 order by 排序 desc) rn from 表名) A where rn = 1
|
删除
1
| DELETE TOP (20) from table 表名 where (条件)
|
1 2 3
| delete from 表名 where id in ( select min(id) from 表名 group by [去除重复的字段名列表,....] having count(1)>1 )
|
1 2 3
| delete A from (select *, row_number() over(partition by 分组字段 order by 排序 desc) rn from 表名) A where rn = 2
|
为了避免导致出现重复数据,还是要建议唯一索引比较好