数据库删除重复记录

参考文章

用SQL语句,删除掉重复项只保留一条

https://www.cnblogs.com/lanliying/p/5695349.html

idcard,time, company

有多条记录,idcard和company一样,但是time不一样,在一天中的不同时间。

删除这些同一天中的重复记录,只保留一条。

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

delete from people 
where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1) 
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)

TODO 怎么根据多个字段来判断重复呢?

一天里,只有一条记录,但是表格里,可以有多条历史记录。

在条件里,增加时间,time between 范围。筛选和删除,都增加时间条件。

delete from people 
where peopleName in (select peopleName from people where time > 20181017130000 and time < 20181017170000 group by peopleName having count(peopleName) > 1) 
and peopleId not in (select min(peopleId) from people where time > 20181017130000 and time < 20181017170000 group by peopleName having count(peopleName)>1)
and time > 20181017130000 and time < 20181017170000 

时间查询

https://www.cnblogs.com/zjdxr-up/p/8383693.html

SELECT * FROM tbl_student_info WHERE `createDate` between '20170101020304' and '20180101020304'    
        等同于:
SELECT * FROM tbl_student_info WHERE `createDate` >= '20170101020304' and `createDate` <= '20180101020304'    

猜你喜欢

转载自blog.csdn.net/GoOnDrift/article/details/82994412