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、創(chuàng)建數(shù)據(jù)庫并插入數(shù)據(jù):
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) #插入多條數(shù)據(jù)
# select出重復(fù)數(shù)據(jù)并刪除
def deleteSameData(delSql,tableName):
mycursor.execute(delSql)
mycursor.execute(str("select * from " +tableName))
myresult1 = mycursor.fetchall()
for x in myresult1:
print(x)
#select出重復(fù)數(shù)據(jù)并刪除
print("select出重復(fù)數(shù)據(jù)并刪除")
#根據(jù)name字段count計算>1表示有重復(fù),只顯示最小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')
總結(jié)
以上是生活随笔為你收集整理的python去重复的数据_Python中mysql查询重复数据并删除重复数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python如何改颜色_python和n
- 下一篇: spark on yarn 完全分布式_