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

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/84669046

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表即可

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/84669046