【问题标题】:MySQL cant join against temp tableMySQL 无法加入临时表
【发布时间】:2015-02-20 18:48:49
【问题描述】:

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

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);

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

mysql> select T.tweet_id, T.favorite_count, T.retweet_count, T2.C from 
-> tweets AS T JOIN table2 AS T2 
-> on T.tweet_id = T2.in_reply_to_id;

错误 1146 (42S02):表“twitter_analysis.table2 as”不存在 mysql>

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

select count(*) from table2;

57149 1 行(0.01 秒)

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

【问题讨论】:

    标签: mysql


    【解决方案1】:

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

    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;
    

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

    【讨论】:

    • @Tommy 确定!很高兴我能帮上忙 :) 您是否尝试从临时表中进行选择并将推文表加入其中?这可能是您尝试的解决方法
    【解决方案2】:

    您可以将它们放在一个查询中:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 2016-07-22
      • 2016-01-31
      • 2018-11-20
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多