Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DELETE FROM HAVING COUNT(*) in MySQL

Ok so there are couple posts here already on this and fewer still out on the web. I've literally tried every one of them and can not get anything to work. Hopefully someone here can take pity on me :)

Here is the data I'm working with. I want to delete all these records.

SELECT
part_desc, count(*) as rec_num
FROM ag_master
GROUP BY part_desc HAVING COUNT(*) > 1000;

+--------------------------------------+---------+
| part_desc                            | rec_num |
+--------------------------------------+---------+
| SILICON DELAY LINE, TRUE OUTPUT      |    1092 |
| LOADABLE PLD                         |    1401 |
| 8-BIT, FLASH, 8 MHz, MICROCONTROLLER |    1411 |
| FPGA                                 |    1997 |
| 8-BIT, MROM, 8 MHz, MICROCONTROLLER  |    3425 |
+--------------------------------------+---------+
5 rows in set (0.00 sec)

The closest I've come to finding code that would do it is shown below. The syntax checks ok and it runs, however it just seems to hang the database up. I've let it run for as long as 10 minutes and nothing ever happens so I abort it.

DELETE
FROM ag_master
WHERE part_id IN (
  SELECT part_id
  FROM ag_master
  GROUP BY part_desc
  HAVING COUNT(*) > 1000
);

Here's the explain plan on the tmp table

mysql> EXPLAIN SELECT * FROM ag_master WHERE part_desc IN (SELECT part_desc FROM tmp);
+----+--------------------+-----------+--------+---------------+------+---------+------+--------+-------------+
| id | select_type        | table     | type   | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+--------------------+-----------+--------+---------------+------+---------+------+--------+-------------+
|  1 | PRIMARY            | ag_master | ALL    | NULL          | NULL | NULL    | NULL | 177266 | Using where |
|  2 | DEPENDENT SUBQUERY | tmp       | system | NULL          | NULL | NULL    | NULL |      1 |             |
+----+--------------------+-----------+--------+---------------+------+---------+------+--------+-------------+
2 rows in set (0.00 sec)
like image 417
RFQ Master Avatar asked May 11 '12 15:05

RFQ Master


People also ask

Can we use having clause with delete?

WHERE Clause can be used with SELECT, UPDATE, DELETE statement. HAVING Clause can only be used with SELECT statement. 6.

How do I delete a record from a MySQL database?

DELETE SyntaxDELETE FROM table_name WHERE condition; Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which record(s) should be deleted.

Can we use delete with Group By?

MySQL Delete with Group By and Having clauses. To achieve this exploit with a single simple MySQL command, we are using two useful functions: 1. GROUP_CONCAT()

How do I delete multiple entries in MySQL?

If you wanted to delete a number of rows within a range, you can use the AND operator with the BETWEEN operator. DELETE FROM table_name WHERE column_name BETWEEN value 1 AND value 2; Another way to delete multiple rows is to use the IN operator.


2 Answers

As stated in the manual:

Currently, you cannot delete from a table and select from the same table in a subquery.

I think you'll have to perform this operation via a temporary table:

CREATE TEMPORARY TABLE temp
  SELECT   part_desc
  FROM     ag_master
  GROUP BY part_desc
  HAVING   COUNT(*) > 1000;

DELETE FROM ag_master WHERE part_desc IN (SELECT part_desc FROM temp);

DROP TEMPORARY TABLE temp;
like image 150
eggyal Avatar answered Oct 09 '22 06:10

eggyal


Another option is using an inner join to filter the result:

DELETE
    ag_master.*
FROM
    ag_master 

    INNER JOIN 
    (
        SELECT 
            part_id
        FROM 
            ag_master
        GROUP BY 
            part_desc
        HAVING COUNT(*) > 1000
    )AS todelete ON
            todelete.part_id = ag_master.part_id
like image 27
Jonathan Heijmans Avatar answered Oct 09 '22 06:10

Jonathan Heijmans