为什么我不能创建一个临时表然后立即对其进行 JOIN?
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);
Query OK, 57149 rows affected (0.14 sec) Records: 57149 Duplicates: 0 Warnings: 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;
ERROR 1146 (42S02): Table 'twitter_analysis.table2 as' doesn't exist mysql>
但它确实存在,因为我可以从中选择!
select count(*) from table2;
57149 1 row in set (0.01 sec)
这个错误非常令人沮丧。 有没有办法将此临时表放入选择查询中?
最佳答案
你可以像这样把它放在查询中
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;
您可以尝试从临时表中选择并将另一个表连接到它。
关于MySQL 无法加入临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606612/