t_main
col1 col2 col3
1 a1 a2
2 b1 b2
3 c1 c2t_list
col1 col4 col5
1 2.3 aa
1 2.6 bb
1 3.5 cc
2 2.2 bb
2 5.0 dd
3 7.6 cc
3 2.8 ee得到样式
col1 col2 col3 aa bb cc dd ee
1 a1 a2 2.3 2.6 3.5
2 b1 b2 2.2 5.0
3 c1 c2 7.6 2.8
col1 col2 col3
1 a1 a2
2 b1 b2
3 c1 c2t_list
col1 col4 col5
1 2.3 aa
1 2.6 bb
1 3.5 cc
2 2.2 bb
2 5.0 dd
3 7.6 cc
3 2.8 ee得到样式
col1 col2 col3 aa bb cc dd ee
1 a1 a2 2.3 2.6 3.5
2 b1 b2 2.2 5.0
3 c1 c2 7.6 2.8
select col1,sum(case when col5='aa' then col4 then NULL end),
sum(case when col5='bb' then col4 then NULL end),
sum(case when col5='cc' then col4 then NULL end),
sum(case when col5='ee' then col4 then NULL end)
from t_list
group by col1)b
where a.col1=b.col1;
但是 表t_list中 列不定咯.
中我的回答
在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义。http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html?96198现整理解法如下:数据样本: create table tx( id int primary key, c1 c...