MySQL 报错:ERROR 1137 (HY000): Can't reopen table: 'tempId'

本文详细解析了在MySQL中使用临时表(temporary table)时遇到的ERROR1137错误,即在一个查询中重复引用同一临时表导致的问题。通过实例展示了错误的SQL语句,并提供了修改建议,确保在一个查询中只引用一次临时表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL 报错:ERROR 1137 (HY000): Can't reopen table: 'tempId'

1. sql 如下
select 
replace(group_concat(distinct tsk.task_name),',' , '\n') as res2
from tempId
join custom_task_rule tsk
  on tsk.task_id = tempId.dependency_job_id
where tempId.dependency_job_id not in (
	select task_id
		from etl_process_log
		where task_id in(
		select
		dependency_job_id
		from tempId
	)   
	and status = 2
	and logging_time > '2018-11-30 00:00:00'
);
ERROR 1137 (HY000): Can't reopen table: 'tempId'

其中tempId是一个临时表。
执行如上SQL时,得到的结果是Can't reopen table:'tempId',然后仔细检查一遍SQL,除了写的丑了一点儿之外,也没有别的错了啊。于是打开mysql refman手册。

2. 原因

手册中关于 temporary table 的介绍如下:

在这里插入图片描述其中较为关键的提示就是:

You cannot refer to a TEMPORARY table more than once in the same query. For example, the following does not work:
SELECT * FROM temp_table JOIN temp_table AS t2;
The statement produces this error:
ERROR 1137: Can't reopen table: 'temp_table'
The Can’t reopen table error also occurs if you refer to a temporary table multiple times in a stored
function under different aliases, even if the references occur in different statements within the function.
It may occur for temporary tables created outside stored functions and referred to across multiple calling
and callee functions.

在一个查询中能且仅能引用一次 temporary 表。 上述的那个查询中将会不work。这个SQL 将会报ERROR 1137: Can't reopen table: 'temp_table'错误。
如果你在一个函数中以不同的别名多次引用相同的临时表,即使在函数不同的语句中引用,这个错误同时会出现。对于在存储函数之外创建的临时表,但又在多个调用和被调用函数中引用,也可能会出现这种情况。

3. 解决办法

所以我们需要修改这个SQL,使其在一个query中只使用到一次tempId表即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

说文科技

看书人不妨赏个酒钱?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值