DELETE FROM HAVING COUNT(*) in MySQL(在 MySQL 中从 HAVING COUNT(*) 中删除)
问题描述
好的,这里已经有几篇关于此的帖子,而网络上的帖子则更少.我真的尝试了其中的每一个,但无法正常工作.希望这里有人可以可怜我:)
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)
我找到的最接近的代码如下所示.语法检查正常并运行,但它似乎只是挂断了数据库.我已经让它运行了长达 10 分钟,但什么也没发生,所以我中止了它.
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
);
这是tmp表上的解释计划
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)
推荐答案
如手册中所述:
目前,您不能从表中删除并从子查询中的同一个表中选择.
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;
这篇关于在 MySQL 中从 HAVING COUNT(*) 中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 MySQL 中从 HAVING COUNT(*) 中删除
基础教程推荐
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
