mysql 清除重复数据
python代码
def clean_table(db: Session):
select_sql = ('SELECT MIN(id) as id,col1,col2,COUNT(*) FROM table '
'GROUP BY col1,col2 '
'HAVING COUNT(*) > 1 LIMIT 200 ')
while True:
select_sql_res = db.execute(select_sql).fetchall()
if not select_sql_res:
break
delete_ids = [str(item["id"]) for item in select_sql_res]
delete_sql = ('DELETE FROM table '
f'WHERE id in ({",".join(delete_ids)}) ')
db.execute(delete_sql)
db.commit()