我有一个名为etablissement_amenities的表
id
etablissement_id
amenities_id
我需要删除我的重复行。 例如,我有这样的重复行。
id etablissement_id amenities_id
1 1 1
2 1 1
3 2 2
4 6 3
5 6 3
我复制了我的桌子,我已经尝试了这个请求
DELETE FROM doublon
WHERE id < ANY (SELECT etablissement_amenities.id
FROM doublon, etablissement_amenities
WHERE doublon.id = etablissement_amenities.id
AND doublon.amenities_id = etablissement_amenities.amenities_id
AND doublon.etablissement_id = etablissement_amenities.etablissement_id);
但我有错误
You can't specify target table 'doublon' for update in FROM clause
任何人都可以帮助我?
谢谢你。
答案 0 :(得分:1)
此查询将希望从doublon
DELETE FROM doublon WHERE id in
( Select id from `doublon` where `id` not in
( Select id
From `doublon`
group by etablissement_id,amenities_id
having Count(etablissement_id) > 1 and Count(amenities_id) > 1
union
Select id
From `doublon`
group by etablissement_id,amenities_id
having Count(etablissement_id) = 1 and Count(amenities_id) = 1
)
)
以下是重复记录 sqlfiddle
答案 1 :(得分:0)
您不能在MySQL的delete
或update
中再次使用同一个表。相反,只需使用join
:
DELETE ea
FROM etablissement_amenities ea JOIN
(select ea.etablissement_id, MIN(ea.id) as minid
from etablissement_amenities ea
group by ea.etablissement_id
) ead
on ea.etablissement_id = ead.etablissement_id and
ea.id > ead.minid;
我不知道doublon
是什么。您的问题陈述只提到一个表etablissement_amenities
。
答案 2 :(得分:0)
在上述查询中没有关于表doublelon的信息。
要删除或更新引用相同表的子查询的表,请尝试将子查询括在临时派生表中,如下所示。
DELETE FROM doublon
WHERE id < ANY (Select * from (SELECT etablissement_amenities.id
FROM doublon, etablissement_amenities
WHERE doublon.id = etablissement_amenities.id
AND doublon.amenities_id = etablissement_amenities.amenities_id
AND doublon.etablissement_id = etablissement_amenities.etablissement_id) as Tmp);
P.s. The answer is to fix the error in above query. It is not about deleting duplicate values.....