热线电话:13121318867

登录
2019-02-25 阅读量: 1093
mysql如何实现excel的“透视”功能?

问题描述:

透视功能是excel最常用的操作之一,对于取数需求来说非常常见,如何利用mysql予以实现呢?


解决思路:

  • 用临时表+表链接的形式即可实现,例如

create temporary table aa as 
(select CustomerID as '客户ID',sum(Quantity) as '2017购买数量' from orderinfo
where year(OrderDate)='2017'
and orderinfo.CustomerID in (select distinct CustomerID from orderinfo where year(OrderDate)='2016')
group by CustomerID);

create temporary table bb as
(select CustomerID as '客户ID',sum(Quantity) as '2016购买数量' from orderinfo
where year(OrderDate)='2016'
group by CustomerID) ;

select aa.客户ID,2016购买数量,2017购买数量 from aa,bb where aa.客户ID=bb.客户ID;
6.0239
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子