mysql删掉重复数据没有id,从表中删除没有pk或id或mysql中唯一列的重复记录

I need to delete all the duplicated records from one of my tables the problem is that there isn't any id or unique or key column so I can't make something like this:

delete from tbl using tbl,tbl t2 where tbl.locationID=t2.locationID

and tbl.linkID=t2.linkID and tbl.ID>t2.ID

because it needs an id column or unique or key column

and I can't make an

ALTER IGNORE TABLE 'mytable' ADD UNIQUE INDEX

because there is information that will be always necessary duplicated but others don't

and I can't make this:

DELETE FROM 'table' WHERE 'field' IN (SELECT 'field' FROM 'table' GROUP BY 'field'HAVING (COUNT('field')>1))

because it will delete all the duplicated and never will leave one

this is an example of my table

+----------+----------------------+-------------+-------------+

| phone | address | name | cellphone |

+----------+----------------------+-------------+-------------+

| 2555555 | 1020 PANORAMA | JUAN CARLOS | 0999999999 | diferent address

| 2555555 | GABRIEL JOSE 1020 | JUAN CARLOS | 0999999999 | good one

| 2555555 | GABRIEL JOSE 1020 | JUAN CARLOS | 0999999999 | duplicated

| 2555555 | C ATARAZANA 1020 | SILVIA | 0777777777 | another good one

| 2555555 | C ATARAZANA 1020 | SILVIA | 0777777777 | another duplicated

| 2555555 | GABRIEL JOSE 1020 | VIOLETA | 0888888888 | diferent person

+----------+----------------------+-------------+-------------+

and this is what I want to leave

+----------+----------------------+--------------+-------------+

| phone | address | name | cellphone |

+----------+----------------------+--------------+-------------+

| 2555555 | 1020 PANORAMA | JUAN CARLOS | 0999999999 |

| 2555555 | GABRIEL JOSE 1020 | JUAN CARLOS | 0999999999 |

| 2555555 | C ATARAZANA 1020 | SILVIA | 0777777777 |

| 2555555 | GABRIEL JOSE 1020 | VIOLETA | 0888888888 |

+----------+----------------------+--------------+-------------+

and I can't truncate or delete the original table because its used 24/7 and has 10000000 records....

Please help me.

解决方案

Adding a unique index (with all the columns of the table) with

ALTER IGNORE TABLE table_name

ADD UNIQUE INDEX all_columns_uq

(phone, address, name, cellphone) ;

Tested in

Note: In version 5.5 (due to a bug in the implementation of fast index creation), the above will work only if you provide this setting before the ALTER:

SET SESSION old_alter_table=1 ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤设置删除按钮删除treeview和mysql数据的某一行: 1. 在TreeView添加删除按钮的,可以使用tkinter库的Button控件实现。 2. 给删除按钮绑定一个点击事件,当用户点击该按钮时,触发对应的函数。 3. 在函数获取当前选的行,可以使用TreeView的selection()方法。 4. 从选的行获取到需要删除数据的主键值。 5. 使用mysql-connector-python模块连接到数据库,并且执行删除语句。 6. 从TreeView删除对应的行。 下面是一个示例代码,可以参考一下: ```python import mysql.connector from tkinter import * from tkinter import ttk # 创建数据库连接 conn = mysql.connector.connect( host="localhost", user="root", password="password", database="test_db" ) # 创建TreeView root = Tk() tree = ttk.Treeview(root) tree.pack() # 在TreeView添加删除按钮的 tree["columns"] = ("name", "age", "action") tree.column("#0", width=0, stretch=NO) tree.column("name", width=100, anchor=W) tree.column("age", width=100, anchor=W) tree.column("action", width=100, anchor=W) tree.heading("#0", text="", anchor=W) tree.heading("name", text="Name", anchor=W) tree.heading("age", text="Age", anchor=W) tree.heading("action", text="Action", anchor=W) # 添加数据到TreeView cursor = conn.cursor() cursor.execute("SELECT * FROM user") for row in cursor.fetchall(): tree.insert("", END, text="", values=row[:-1] + ("Delete",)) # 删除函数 def delete(): # 获取当前选的行 selection = tree.selection() # 获取需要删除数据的主键值 for item in selection: pk = tree.item(item, "values")[0] # 连接数据库,并且执行删除语句 cursor = conn.cursor() cursor.execute("DELETE FROM user WHERE id=%s", (pk,)) conn.commit() # 从TreeView删除对应的行 tree.delete(item) # 给删除按钮绑定点击事件 tree.bind("<Button-1>", lambda event: delete() if tree.identify_region(event.x, event.y) == "cell" else None) root.mainloop() ``` 在这个示例,我们创建了一个具有删除按钮的TreeView,当用户点击删除按钮时,会触发delete()函数。在该函数,我们首先从选的行获取需要删除数据的主键值,然后连接到mysql数据库,并且执行删除语句。最后,我们从TreeView删除对应的行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值