使用Group By删除MySQL

时间:2011-06-09 16:23:31

标签: mysql group-by

我正在MySQL 5.5中成功运行查询

SELECT columnA
FROM
  table
GROUP BY
  columnA
HAVING
  count(*) > 1

但是,我需要使用DELETE运行相同的查询,我有点不确定如何删除?即应删除返回的结果?

有什么想法吗?

2 个答案:

答案 0 :(得分:26)

将其放在子查询中:

delete from table 
where columnA in (
  select columnA
  from (
      select columnA
      from YourTable
      group by columnA
      having count(*) > 1
      ) t  
)

答案 1 :(得分:2)

delete from YourTable
where
  YourTable.columnA 
  in 
  (select columnA
  from
    YourTable
  group by
    column A
  having
    count(*) > 1)