为了能到远方,脚下的每一步都不能少.|

bitterteaer

园龄:3年8个月粉丝:1关注:0

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

本文作者:bitterteaer

本文链接:https://www.cnblogs.com/bitterteaer/p/18156354

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   bitterteaer  阅读(4)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起