MySQL 无法连接临时表

Tom*_*mmy 1 mysql

为什么我不能创建临时表然后立即加入它?

\n\n
mysql> CREATE TEMPORARY TABLE table2 as (select in_reply_to_id, COUNT(in_reply_to_id) as C from mentions where in_reply_to_id>0 group by in_reply_to_id);\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n
\n

查询正常,57149 行受影响(0.14 秒)\n 记录:57149 重复:0 警告:0

\n
\n
\n\n
mysql> select T.tweet_id, T.favorite_count, T.retweet_count, T2.C from \n-> tweets AS T JOIN table2\xc2\xa0AS T2 \n-> on T.tweet_id = T2.in_reply_to_id;\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n
\n

错误 1146 (42S02): 表 'twitter_analysis.table2\xc2\xa0as' 不存在\n mysql>

\n
\n
\n\n

但它确实存在,因为我可以从中选择!

\n\n
select count(*) from table2;\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n
\n

57149 1 行一组(0.01 秒)

\n
\n
\n\n

这个错误非常令人沮丧。\n有没有办法将此临时表放入选择查询中?

\n

Joh*_*ell 5

你可以像这样把它放在查询中

select T.tweet_id, T.favorite_count, T.retweet_count, T2.C 
FROM tweets T 
JOIN 
(   SELECT in_reply_to_id, COUNT(in_reply_to_id) as C 
    FROM mentions 
    WHERE in_reply_to_id>0 
    GROUP BY in_reply_to_id
) T2 on T.tweet_id = T2.in_reply_to_id;
Run Code Online (Sandbox Code Playgroud)

您可以尝试从临时表中进行选择并将另一个表加入其中。