[英]How I can perform transpose operation in MySQL table data and columns?
For instance, I have a table named 'stdinfo' like例如,我有一个名为“stdinfo”的表,例如
Id Name Mark
-----------------
1 Helal 10
2 Shakil 15
... ... ...
... ... ...
... ... ...
now I want transform the table result like below现在我想像下面这样转换表格结果
Name Helal Shakil ...
----------------------
Id 1 2 ...
Mark 10 15 ...
There is one important thing is that the number of rows is not fixed.有一件重要的事情是行数不是固定的。
You can try this你可以试试这个
Table桌子
--------------------------------------------------
subject_id subjectname classid teacher_id
---------------------------------------------------
3 Math 3 T-1
4 Economics 4 T-1
5 Physcis 3 T-1
--------------------------------------------------
Query:询问:
SELECT sum( if( subjectname = 'Math', subject_id, 0 ) ) AS 'Economics',
sum( if( subjectname = 'Economics', subject_id, 0 ) ) AS 'Math',
sum( if( subjectname = 'Physcis', subject_id, 0 ) ) AS 'Physcis'
FROM tbsubject group by subjectname
Output:输出:
----------------------------
Math Economics Physcis
---------------------------
0 4 0
3 0 0
0 0 5
----------------------------
Or you can create a view by programming.more example see pivot table http://www.krishnasunuwar.com.np/2011/02/crosstab-query-pivot-table-or-transformation-of-rows-into-columns/或者您可以通过编程创建视图。更多示例请参见数据透视表http://www.krishnasunuwar.com.np/2011/02/crosstab-query-pivot-table-or-transformation-of-rows-into-columns/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.