我有一个名为scheduler的表。它包含以下列:
ID
sequence_id
schedule_time (timestamp)
processed
source_order
我需要从表中删除重复的行,但保留1行具有相同schedule_time和source_order的特定sequence_id,其中processed = 0
答案 0 :(得分:1)
DELETE yourTable FROM yourTable LEFT OUTER JOIN (
SELECT MIN(ID) AS minID FROM yourTable WHERE processed = 0 GROUP BY schedule_time, source_order
) AS keepRowTable ON yourTable.ID = keepRowTable.minID
WHERE keepRowTable.ID IS NULL AND processed = 0
我从这篇文章申请; P How can I remove duplicate rows? 你见过吗?
- 修正版 -
DELETE yourTable FROM yourTable LEFT OUTER JOIN (
SELECT MIN(ID) AS minID FROM yourTable WHERE processed = 0 GROUP BY schedule_time, source_order
) AS keepRowTable ON yourTable.ID = keepRowTable.minID
WHERE keepRowTable.minID IS NULL AND processed = 0
答案 1 :(得分:0)
对于mysql
DELETE a from tbl a , tbl b WHERE a.Id>b.Id and
a.sequence_id= b.sequence_id and a.processed=0;
答案 2 :(得分:0)
删除重复项的最快方法 - 绝对是通过添加索引强制它们,只留下表中每个左边的一个副本:
ALTER IGNORE TABLE dates ADD PRIMARY KEY (
ID
sequence_id
schedule_time
processed
source_order
)
现在,如果你有一个密钥,你可能需要删除它等等,但重点是当你把一个带有IGNORE的唯一密钥添加到一个带有重复项的表时 - 该行为是删除所有额外的记录/重复项。因此,在添加此密钥后,您现在只需要再次删除它以便能够创建新的重复项: - )
现在,如果您需要进行更复杂的过滤(关于其中一个重复过滤,以保持您不能只包含在索引中 - 尽管不太可能),您可以在选择的同时创建一个表并输入您的内容想要它 - 所有在同一个查询中:
CREATE TABLE tmp SELECT ..fields.. GROUP BY ( ..what you need..)
DROP TABLE original_table
ALTER TABLE tmp RENAME TO original_table_name