在执行类似于以下内容的查询时,我正在接收一个错误ERROR 1137 (HY000) at line 9: Can't reopen table: 'temp_table':
USE database_name;
CREATE TEMPORARY TABLE temp_table (status varchar(20));
INSERT INTO temp_table (status)
SELECT status
FROM client_contractor_status
WHERE type = 'process';
SELECT table1.col1,
table1.status,
(SELECT
COUNT(*)
FROM
table2
RIGHT OUTER JOIN
temp_table
ON table2.status = temp_table.status
WHERE table2.col1 = table1.col1
) AS counter
FROM
table1
RIGHT OUTER JOIN
temp_table
ON table1.status = temp_table.status我(几乎)意识到这样的限制,即不能用两个不同的别名访问临时表,但是我根本没有对它进行别名化。
是因为它在子查询中而自动别名吗?如果是的话,如何才能解决这个问题?
谢谢。
发布于 2013-06-26 11:05:42
这是一个已知的有据可查问题:
TEMPORARY表。例如,以下操作不起作用: mysql> SELECT * FROM temp_table,temp_table AS t2;error 1137:无法重新打开表:“temp_table”--如果在存储的函数中多次引用不同别名下的临时表,即使引用发生在函数中的不同语句中,也会发生此错误。想到的一个解决办法是使用“正常”表作为临时存储。您可能需要使用会话标识符来使该方法在多用户环境中工作。
发布于 2023-02-02 07:37:20
您可以在第一个查询中始终使用另一个临时表作为缓冲区,然后使用该缓冲表重新填充它。例:
-- Insert data into a temp buffer table
insert into tmp_buffer_table(id, name)
select per_t.id, per_t.name from tmp_table t, permanent_table per_t where per_t.id = t.id;
-- copy data form the temp buffer table to actual temp table
insert into tmp_table(id, name) select id, name from tmp_buffer_table;
-- reset the buffer table
truncate table tmp_buffer_table;https://dba.stackexchange.com/questions/45270
复制相似问题