删除重复的SQL记录以允许使用唯一密钥
我在MYSQL数据库中有一个表('sales'),它应该有一个唯一的强制约束,以防止重复。事实证明,首先移除重复并设置约束有点棘手 表结构(简化):删除重复的SQL记录以允许使用唯一密钥,sql,mysql,join,Sql,Mysql,Join,我在MYSQL数据库中有一个表('sales'),它应该有一个唯一的强制约束,以防止重复。事实证明,首先移除重复并设置约束有点棘手 表结构(简化): “id(唯一,自动公司)” 产品标识 目标是强制产品id的唯一性。我要应用的重复数据消除策略是删除所有重复记录,但最近创建的记录除外,例如:最高id 或者换句话说,我只想删除重复记录,不包括以下查询匹配的ID,同时保留现有的非重复记录: select id from sales s inner join (select product
- “id(唯一,自动公司)”
- 产品标识
select id
from sales s
inner join (select product_id,
max(id) as maxId
from sales
group by product_id
having count(product_id) > 1) groupedByProdId on s.product_id
and s.id = groupedByProdId.maxId
我在两个方面遇到了困难——编写查询以选择要删除的正确记录,然后在MYSQL中还有一个约束,即delete的subselect FROM子句不能引用要从中删除数据的同一个表
我查看了答案,它似乎处理了这个主题,但似乎特定于sql server,尽管我不会排除重复另一个问题的可能性。在回答您的评论时,这里有一个在MySQL中工作的查询:
delete YourTable
from YourTable
inner join YourTable yt2
on YourTable.product_id = yt2.product_id
and YourTable.id < yt2.id
我可以在sql server中执行以下操作以消除重复项:
DELETE FROM Sales
FROM Sales
INNER JOIN Sales b ON Sales.product_id = b.product_id AND Sales.id < b.id
从销售中删除
来自销售
Sales.product\u id=b.product\u id和Sales.id
mysql的类似语句可能是:
DELETE FROM Sales
USING Sales
INNER JOIN Sales b ON Sales.product_id = b.product_id AND Sales.id < b.id
从销售中删除
使用销售
Sales.product\u id=b.product\u id和Sales.id
使用CTE和排名函数更容易解决此类问题,但是,您应该能够执行以下操作来解决问题:
Delete Sales
Where Exists(
Select 1
From Sales As S2
Where S2.product_id = Sales.product_id
And S2.id > Sales.Id
Having Count(*) > 0
)
也许可以使用
alterignoretable。。。添加唯一键
。
例如:
describe sales;
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| product_id | int(11) | NO | | NULL | |
+------------+---------+------+-----+---------+----------------+
select * from sales;
+----+------------+
| id | product_id |
+----+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 3 |
| 6 | 2 |
+----+------------+
ALTER IGNORE TABLE sales ADD UNIQUE KEY idx1(product_id), ORDER BY id DESC;
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 3 Warnings: 0
select * from sales;
+----+------------+
| id | product_id |
+----+------------+
| 6 | 2 |
| 5 | 3 |
| 2 | 1 |
+----+------------+
有关更多信息,请参见此
请注意,
id
s以相反的顺序结束。我认为这无关紧要,因为id
s的顺序在数据库中应该无关紧要(据我所知!)。然而,如果这让你不高兴,上面链接的帖子也展示了解决这个问题的方法。但是,它需要创建一个临时表,它需要比我上面发布的就地方法更多的硬盘空间 是的,当你在我的评论后发布你的修订答案时,我正在尝试,安德奥马尔。感谢你们两位。如果我无法删除或更新父行,该怎么办:外键约束失败('db.'AnotherTable',约束'AnotherTable_fk'外键('YourTable.product_id')在删除时引用'YourTable'('product_id'))
?
describe sales;
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| product_id | int(11) | NO | | NULL | |
+------------+---------+------+-----+---------+----------------+
select * from sales;
+----+------------+
| id | product_id |
+----+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 3 |
| 6 | 2 |
+----+------------+
ALTER IGNORE TABLE sales ADD UNIQUE KEY idx1(product_id), ORDER BY id DESC;
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 3 Warnings: 0
select * from sales;
+----+------------+
| id | product_id |
+----+------------+
| 6 | 2 |
| 5 | 3 |
| 2 | 1 |
+----+------------+