1. 程式人生 > >全表去重,欄位有null值,保留最大id的資料

全表去重,欄位有null值,保留最大id的資料

delete  from budget_history_report  where exists 
(select * from (select year ,season,schoolId,ifnull(gradeId, '') gradeId,ifnull(subjectId, '') subjectId,count(*),max(id) id from budget_history_report
 group by  year ,schoolId,season,gradeId,subjectId having count(*) > 1) t2 
where budget_history_report.year = t2.year
and budget_history_report.season = t2.season 
and budget_history_report.schoolId = t2.schoolId 
and ifnull(budget_history_report.gradeId, '') = t2.gradeId 
and ifnull(budget_history_report.subjectId, '') = t2.subjectId
 and budget_history_report.id<t2.id);