1.查出所有重復的數據(多列),根據 datetime,staff_num,state
select * from gs_collect_staff a where (a.datetime,a.staff_num,a.state) in (select datetime,staff_num,state from gs_collect_staff group by staff_num,datetime,state having count(*) > 1 ) order by datetime desc
2.查收所有重復的數據(多列),只顯示id最小的那個值
select * from gs_collect_staff a where (a.datetime,a.staff_num,a.state) in (select datetime,staff_num,state from gs_collect_staff group by staff_num,datetime,state having count(*) > 1 ) and a.id in(select min(id)from gs_collect_staff group by staff_num,datetime,state having count(*) > 1 ) order by datetime desc
3.刪除重復的數據(多列),只保留id最小的那個值
delete from gs_collect_staff a where (a.datetime,a.staff_num,a.state) in (select datetime,staff_num,state from gs_collect_staff group by staff_num,datetime,state having count(*) > 1 ) --order by datetime desc and a.id not in(select min(id)from gs_collect_staff group by staff_num,datetime,state having count(*) > 1 )
轉:https://www.cnblogs.com/wangfuyou/p/6058169.html
