year month moment
2015 1 1.21
2015 1 1.22
2015 1 1.23
2015 1 1.24
2015 2 2.21
2015 2 2.22
2015 2 2.21
2015 2 2.22
查询结果显示为
year month m1 m2 m3 m4
2015 1 1.21 1.22 1.23 1.24
2015 2 2.21 2.22 2.23 2.24
select year,month, decode(month,'1',moment),decode(month,'2',moment) from table
列转行,行转列。我只是一个路过的围观群众
select t.year as year,
t.month as month,
MAX(CASE t.moment WHEN 1.21 THEN t.moment ELSE 0 END ) as m1,
MAX(CASE t.moment WHEN 1.22 THEN t.moment ELSE 0 END ) as m2,
MAX(CASE t.moment WHEN 1.23 THEN t.moment ELSE 0 END ) as m3,
MAX(CASE t.moment WHEN 1.24 THEN t.moment ELSE 0 END ) as m4
from table t group by t.month,t.moment;
参考自:
MySQL行转列、列转行举例 http://www.data.5helpyou.com/article390.html