我有一个 mysql 表。 header 的结构如下:
字段 字段 字段 字段 供应商
数据 数据 数据 数据 数据
数据 数据 数据 数据 数据
数据 数据 数据 数据 数据2
数据 数据 数据 数据 数据2
数据 数据 数据 数据 data2
现在,我想删除共享相同供应商值的重复行。因此,不应将相同但具有不同供应商列值的两行视为重复。但是,等于并共享相同供应商值的两行或更多行将被视为重复项,应将其删除,只留下一个现有副本。
如何使用 mysql 完成此操作?
我做了类似的事情,但它删除了所有内容,而不仅仅是重复的值。我想要至少保留一份副本。
DELETE
从自动 WHERE ((auto.email='carolzvalentine@yahoo.com') 和 (auto.vendors='5552'))
最佳答案
您可以使用 ALTER IGNORE
在表的列上添加 UNIQUE
索引:
ALTER IGNORE TABLE mytbl ADD UNIQUE INDEX (field1, field2, field3, field4, vendors)
如 the manual 中所述:
IGNORE
is a MySQL extension to standard SQL. It controls howALTER TABLE
works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. IfIGNORE
is not specified, the copy is aborted and rolled back if duplicate-key errors occur. IfIGNORE
is specified, only the first row is used of rows with duplicates on a unique key. The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value.
这也将防止将来添加重复项(如果你想允许这样,你可以在索引创建后DROP
)。
关于mysql - 删除在第 n 列中也具有相同值的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11743773/