孙健以前发布过一篇文章,大家可以参考一下,但是如果MySQL数据量太大了,到第二步就会卡死,所以孙健决定利用python来删除,但是时间上会慢一点。
wordpress删除重复标题文章的简单代码
首先,利用sql语句建立一个临时表
CREATE TABLE my_tmp AS SELECT MIN(ID) AS col1 FROM article_data GROUP BY title;
第二步,需要在Python环境下进行了,如下是python代码:
import time
import pym ysql
if __name__=='__main__':
db = pym ysql.connect("127.0.0.1","root","root","sites" )
cursor = db.cursor()
sql="select * from article_data "
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
sql_1="select col1 from my_tmp where col1="+str(row[0])
cursor.execute(sql_1)
r1=cursor.fetchall()
if(len(r1)==0):
sql_2="delete from article_data where id="+str(row[0])
cursor.execute(sql_2)
db.commit()
print(str(row[0])+"成功删除")
else:
print(str(r1)+"没有问题,跳过")
time.sleep(0.1)
db.close()
然后,就运行程序吧,它是一步一步执行的,不像sql语句一步到位,所以会慢一点

孙健个人博客