我继承了一个缺乏唯一约束的数据库,因此数据被/被复制。我现在正在尝试删除重复的记录,然后添加一个约束来阻止这一点。
我有这个查询:
SELECT count(*) as dacount, substr(group_concat(id), (locate(',', group_concat(id))+ 1))
FROM `game`
group by matchid, ordinal
having dacount > 1
order by dacount desc
Run Code Online (Sandbox Code Playgroud)
这正确地给了id
我需要删除的行的s。但问题是,DELETE
由于dacount
withhaving
参数,我不能将其用作子查询。有没有另一种方法可以做到这一点?
这是我的计划:
DELETE FROM game WHERE id IN (SELECT count(*) as dacount, substr(group_concat(id), (locate(',', group_concat(id))+ 1))
FROM `game`
GROUP BY matchid, ordinal
HAVING dacount > 1)
Run Code Online (Sandbox Code Playgroud)