日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python去重复的数据_Python中mysql查询重复数据并删除重复数据

發布時間:2024/9/15 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python去重复的数据_Python中mysql查询重复数据并删除重复数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#coding:utf-8

import mysql.connector

mydb = mysql.connector.connect(

host='localhost',

user='root',

passwd='123456',

database='mydatabase',

buffered = True

)

print(mydb)

#1、創建數據庫并插入數據:

mycursor = mydb.cursor()

sql = "insert into customers(name,address) values(%s,%s)"

val =("John","London 21")

mycursor.execute(sql,val)

mydb.commit()

print(mycursor.rowcount,"record inserted")

val2 = [

('Peter', 'Lowstreet 4'),

('Amy', 'Apple st 652'),

('Hannah', 'Mountain 21'),

('Michael', 'Valley 345'),

('Sandy', 'Ocean blvd 2'),

('Betty', 'Green Grass 1'),

('Richard', 'Sky st 331'),

('Susan', 'One way 98'),

('Vicky', 'Yellow Garden 2'),

('Ben', 'Park Lane 38'),

('William', 'Central st 954'),

('Chuck', 'Main Road 989'),

('Viola', 'Sideway 1633')

]

insertFn(mycursor,'customers','name,address',val2) #插入多條數據

# select出重復數據并刪除

def deleteSameData(delSql,tableName):

mycursor.execute(delSql)

mycursor.execute(str("select * from " +tableName))

myresult1 = mycursor.fetchall()

for x in myresult1:

print(x)

#select出重復數據并刪除

print("select出重復數據并刪除")

#根據name字段count計算>1表示有重復,只顯示最小id的客戶信息

sql="select * from customers where id in (select min(id) from customers group by name having count(id)>1)"

delSql="delete from a using customers as a where a.id not in (select * from (select min(id) from customers group by name having count(id)>1) a)"

deleteSameData(delSql,'customers')

總結

以上是生活随笔為你收集整理的python去重复的数据_Python中mysql查询重复数据并删除重复数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。