日期:2014-05-16  浏览次数:20809 次

怎么将行变成列
比如行 select * from table
ID   A1   B1   C1   D1

变成列
A1
B1
C1
D1

应该怎么做呢
------解决方案--------------------
90°旋转。。。
------解决方案--------------------
select a1 from table
union all
select b1 from table
union all
select c1 from table
union all
select d1 from table

------解决方案--------------------
引用:
select a1 from table
union all
select b1 from table
union all
select c1 from table
union all
select d1 from table

正解
------解决方案--------------------
请注意,你这个不叫行转列,叫列转行。
------解决方案--------------------
一般出现这种问题,都是数据库结构设计太随意,特征是列很多,数据又很稀疏
设计的时候就应该列转行
------解决方案--------------------
而且数据库结构还不稳定,用户提出新要求,可能你就需要修改表结构,增加新列