朝阳Tim

2019-02-21   阅读量: 2422

Mysql

mysql子查询无法重复使用临时表的解决方法?

扫码加入数据分析学习群

问题描述:

当我们在使用mysql进行数据查取的时候,往往会涉及临时表的功能,但是当一个query语句中,如果使用了多个临时表,会报错1146,对于这个问题应该如何解决?


解决思路:

  • 可以采取创建多个临时表的思路予以解决,也就是把子查询结果封装为新的临时表(create temporary table xxx as (子查询))
#drop temporary table tem;


create temporary table tem(
       OrderID int,
    Userid int,
    primary key(OrderID,Userid)
);


insert into tem
       select o.OrderID,o.UserID from orderinfo o;
   
select * from tem limit 3;
select * from tem_new limit 3;


create temporary table tem_new as(select OrderID from new_tem order by OrderID limit 20,3) ;


select *
       from tem
       inner join tem_new using (OrderID);#on tem.OrderID=tem_new.OrderID
添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
0.0000 0 3 关注作者 收藏

评论(0)


暂无数据

推荐课程