distinct mysql更新 插入 删除 操作

sql= "select distinct Novel_url from `qishu_books_sort011` GROUP BY Novel_url ORDER BY Novel_ID ASC;"


删除重复的数据

print('MySQL数据库正在连接,请稍等。。。。。。')

db= pymysql.connect('27.255.79.21','user','******!','db_qishu',charset="utf8")

#db = pymysql.connect('127.0.0.1', 'root', 'root', 'db_qishu', charset="utf8")

cursor= db.cursor()

print('MySQL数据库已经连接!!!!!!')

#sql = "SELECT Novel_url,count(Novel_url) as count FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY count ASC;"

#sql = "select Novel_ID,count(*) from qishu_books_sort08 group by Novel_url having count(Novel_url) > 1;"

#sql= "SELECT Novel_ID FROM `qishu_books_sort08` GROUP BY Novel_url ORDER BY Novel_ID ASC;"

#sql = "select Novel_ID,Novel_title from qishu_books_sort08 where Novel_url IN(SELECT Novel_url FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY Novel_ID ASC);"

#sql = "select Novel_ID from qishu_books_sort08 where Novel_ID NOT IN(SELECT min(Novel_ID) FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY Novel_ID ASC);"

#sql = "select Novel_ID from qishu_books_sort08 where Novel_ID NOT IN(SELECT min(Novel_ID) FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY Novel_ID ASC);"

sql= "SELECT COUNT(Novel_ID) FROM qishu_books_sort08;"

#sql = "SELECT Novel_ID FROM qishu_books_sort08 WHERE Novel_url IN(SELECT Novel_url FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY Novel_ID ASC) AND Novel_ID NOT IN(SELECT min(Novel_ID) FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY Novel_ID ASC);"

# sql = "delete from qishu_detail_sort01 WHERE TXT_ID = 5322;"

cursor.execute(sql)

db.commit()

NoveID_list= cursor.fetchall()

print(len(NoveID_list))


try:

    db= pymysql.connect('27.255.79.21','user','******!','db_qishu',charset="utf8")

cursor= db.cursor()

insert_color= ("INSERT INTO qishu_books_sort01(Novel_title,Novel_url,Novel_pid,Novel_name,Novel_model,Novel_ppid,Novel_NewTime)" "VALUES (%s,%s,%s,%s,%s,%s,%s)")

data_color= (Novel_title, Novel_url, Novel_pid, Novel_name, Novel_model,Novel_ppid, Novel_NewTime)

cursor.execute(insert_color, data_color)

db.commit()

except Exception as ex:

    print(Exception,":", ex)

你可能感兴趣的:(distinct mysql更新 插入 删除 操作)