MySQL ERROR 1137 (HY000) 在第 9 行:无法重新打开表:'temp_table'

Sea*_*rey 5 mysql-5.1

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
Run Code Online (Sandbox Code Playgroud)

我(差不多)知道不能通过两个不同的别名访问临时表的限制,但是我根本没有给它设置别名。

是否因为它在子查询中而自动别名?如果是这样,如何解决?

谢谢。

And*_*y M 7

这是一个已知且有据可查的问题:

  • TEMPORARY在同一个查询中不能多次引用一个表。例如,以下不起作用:

    mysql> SELECT * FROM temp_table, temp_table AS t2;
    ERROR 1137: Can't reopen table: 'temp_table'
    
    Run Code Online (Sandbox Code Playgroud)

    如果您在不同别名下的存储函数中多次引用临时表,即使引用出现在函数内的不同语句中,也会发生此错误。

想到的一种解决方法是使用“普通”表作为临时存储。您可能需要使用会话标识符来使该方法在多用户环境中工作。