mysql 清除重複資料

bitterteaer發表於2024-04-22

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()

相關文章