【问题标题】:MySQL 'cant reopen (temporary) table)MySQL'无法重新打开(临时)表)
【发布时间】:2020-07-09 01:53:51
【问题描述】:

尝试通过加入内联查询的结果来规范化列 (wgt),如下所示:

select a.monthEnd, ticker, wgt/totWgt 
from hold a
inner join ( 
            select monthEnd, sum(wgt*(1+totRet)) as totWgt
            from hold
            group by monthEnd ) tot
            on a.monthEnd = tot.monthEnd

得到以下错误:

Error Code: 1137. Can't reopen table: 'a' 0.00023 sec

我来自 SQL Server,并不熟悉。这里的问题和/或解决方法到底是什么?引用的两个表都是在存储过程中创建并运行 MySQL 8.0 的临时表。

【问题讨论】:

  • 在同一个查询中好像不能多次引用临时表,请使用 WITH 子句

标签: mysql mysql-8.0


【解决方案1】:

请使用WITH子句,

WITH hold as 
(SELECT monthEnd, wgt, totWgt , totRet, ticker FROM <actual table>)
select a.monthEnd, ticker, wgt/totWgt 
from hold a inner join 
(select monthEnd, sum(wgt*(1+totRet)) as totWgt from hold group by monthEnd ) tot
on a.monthEnd = tot.monthEnd;

【讨论】:

  • 不要创建CREATE TEMPORARY TABLE hold select * from TABLE;,请使用WITH hold as (SELECT monthEnd, ticker, wgt, totWgt from TABLE),并在后面的地方使用引用。相应地编辑了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2010-09-25
  • 2020-07-08
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多