MySQL去重并保留id最小的数据

'''
只需要修改数据库连接即可
'''
import pymysql
import requests

connect = pymysql.Connect(
    host='140.210.4.73',
    port=3306,
    user='twipad_cj',
    passwd='bj@#twipad_cj',
    db='tw_ipaddb',
    charset='utf8mb4'
)
cursor = connect.cursor()
sql2 = 'select ir_id,ir_authors from hkby_facebookhistory_abroaddataall where ir_content in (select A.ir_content from((select ir_content from hkby_facebookhistory_abroaddataall group by ir_content having count(ir_content) > 1) as A)) ORDER BY ir_content;'
connect.ping(reconnect=True)
cursor.execute(sql2)
list1 = cursor.fetchall()
list2 = []
for i in list1:
    list2.append(i[1])

list3 = []
for i in list1:
    list3.append(i[0])

list4 = []
for k in list2:
    if k not in list4:
        list4.append(list2.index(k))

list5 = []
for l in list4:
    if l not in list5:
        list5.append(l)

list6 = [m for m in range(len(list2))]
list7 = set(list6) - set(list5)

keyword_list = []
for n in list7:
    keyword_list.append(list3[n])
print(f'keyword_list={keyword_list}')
for keyword_list in keyword_list:
    url = f'http://twipad.hnxinxiudata.top/api/data/del_data?tablename=hkby_facebookhistory_abroaddataall&id={keyword_list}'
    html = requests.get(url)
    print(html)

    sql = f'delete from hkby_facebookhistory_abroaddataall where ir_id={keyword_list}'

    data1 = cursor.execute(sql)

    connect.commit()

connect.close()

 

posted @ 2022-02-23 11:38  布都御魂  阅读(203)  评论(0编辑  收藏  举报