Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Delete with Group By

Tags:

I am running a query successfully using in MySQL 5.5

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

However, I need to run this same query using DELETE and I'm a little unsure how to delete ? i.e. the returned results should be deleted ?

Any ideas ?

like image 636
Tom Avatar asked Jun 09 '11 16:06

Tom


People also ask

Can we use group by with delete in SQL?

SQL delete duplicate Rows using Group By and having clause In this method, we use the SQL GROUP BY clause to identify the duplicate rows. The Group By clause groups data as per the defined columns and we can use the COUNT function to check the occurrence of a row.

How can you delete one or multiple columns from a MySQL database table?

ALTER TABLE tbl_Country DROP COLUMN IsDeleted, DROP COLUMN CountryName; This allows you to DROP , ADD and ALTER multiple columns on the same table in the one statement. From the MySQL reference manual: You can issue multiple ADD , ALTER , DROP , and CHANGE clauses in a single ALTER TABLE statement, separated by commas.


2 Answers

Place it in a subquery:

delete from table  where columnA in (   select columnA   from (       select columnA       from YourTable       group by columnA       having count(*) > 1       ) t   ) 
like image 149
Denis de Bernardy Avatar answered Oct 23 '22 16:10

Denis de Bernardy


delete from YourTable where   YourTable.columnA    in    (select columnA   from     YourTable   group by     column A   having     count(*) > 1) 
like image 33
GolezTrol Avatar answered Oct 23 '22 16:10

GolezTrol