2017-02-24 66 views
1

如何将此表格转换在MySQL怎么行转换成列在MySQL

month | Number_oF_User 
1 |  3 
2 |  13 
3 |  2 
12 |  3 

转换到

month    1 2  3  12 
number_of_user 3 13 2   3 
+1

请参阅http://stackoverflow.com/questions/7674786/mysql-数据透视表 –

+0

使用案例陈述和动态查询做现有数据的支点 –

+0

您使用的是ny过程,或者你想单个语句查询 –

回答

1

,你必须使用你的查询是这样的:

选择* 从 ( )选择'number_of_user'为Itm,[1],[2],[3],[4],[5],[6],[7],[8],[9], [10],[11],[12] 从 ( 选择月份(你的专栏)为星期一,你Number_of_User列 从你的表名T1 --where你的条件

) as SourceTable 
Pivot 
(
    Your Number_of_User Column 
    For Mon in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) 
) as Your TableName t1 

)一个 - 这将被应用,如果你想每行的总和横向 交叉应用(选择SUM(Camount)作为SumVal,AVG(Camount)作为AvgVal从paymentreciept我在哪里i.PaymentType = a.Itm)b