此代码不适用于MySQL 5.0,如何重写使其正常工作
DELETE FROM posts where id=(SELECT id FROM posts GROUP BY id HAVING ( COUNT(id) > 1 ))
我想删除没有唯一ID的列。我会在大多数情况下添加唯一的一个ID(我尝试了in语法,它也不起作用)。
Answers:
SELECT id FROM posts GROUP BY id HAVING ( COUNT(id) > 1 )
DELETE
p1
FROM posts AS p1
CROSS JOIN (
SELECT ID FROM posts GROUP BY id HAVING COUNT(id) > 1
) AS p2
USING (id)
CROSS JOIN
显然执行笛卡尔联接,所以看起来这可能会做不必要的工作,或者执行效果不佳?谁能解释?
USING
子句时,它才会做笛卡尔乘积。由于USING
产品仅限于在id
列中具有相同值的对,因此实际上非常有限。
DELETE p1 FROM posts AS p1 INNER JOIN ( SELECT ID FROM posts GROUP BY id HAVING COUNT(id) > 1 ) AS p2 ON p2.ID=p1.ID