我有一张表“c31805”,看起来像这样:
| Id3| COL 1 |<br/>
|----|--------|<br/>
| 1 | trial1 |<br/>
| 2 | x142 |<br/>
| 3 | left |<br/>
| 4 | left |<br/>
| 5 | left |<br/>
| 6 | trial2 |<br/>
| 7 | x139 |<br/>
| 8 | left |<br/>
| 9 | left |<br/>
| 10 | trial3 |<br/>
|----|--------|<br/>
有没有办法在这种情况下删除重复的“左”,它们是直接相互跟随的。但不是后来的那些。 最终结果应如下所示:
| Id3| COL 1 |<br/>
|----|--------|<br/>
| 1 | trial1 |<br/>
| 2 | x142 |<br/>
| 3 | left |<br/>
| 6 | trial2 |<br/>
| 7 | x139 |<br/>
| 8 | left |<br/>
| 10 | trial3 |<br/>
|----|--------|<br/>
我尝试过选择来过滤重复项:
`SELECT `Id3`
from c31805 a1
where 0 < (select count(*) from c31805 a2 where a2.Id3 = a1.Id3 + 1 and a2.`COL 1` = a1.`COL 1`);
这似乎有效。
然后我尝试添加删除:
DELETE FROM c21705
where id3 in (SELECT `Id3` from c21705 m1 where 0 < (select count(*) from c31805 m2 where m2.Id3 = m1.Id3 + 1 and m2.`COL 1` = m1.`COL 1`));
但是这会导致错误消息:
“#1093 - 表'c21705'被指定两次,既作为'DELETE'的目标,也作为单独的数据来源”
有没有办法实现这个目标。
答案 0 :(得分:1)
我认为您必须使用别名来避免混淆,
您的查询将如下所示。
DELETE `t` FROM c21705 as `t` where t.id3 in (SELECT Id3 from c21705 m1 where 0 < (select count(*) from c31805 m2 where m2.Id3 = m1.Id3 + 1 and m2.COL 1= m1.COL 1));
答案 1 :(得分:0)
下面的查询将保留具有最低Id3值的重复值
DELETE FROM c31805
WHERE Id3 NOT IN (SELECT Id3 FROM t
( SELECT [COL 1], MIN(Id3) as Id3
FROM c31805
GROUP BY [COL 1]
) t