一维表变二维表。
数据库里面的值
1 50 a 1 22 b 1 33 c 2 11 a 2 12 b 2 24 c
我要做成这样的 a b c 1 50 22 33 2 11 12 24
你知道这样的sql怎么写吗?
mysql> select * from teamtest; +------+------+------+ | id | name | type | +------+------+------+ | 1 | mao | a | | 1 | da | b | | 1 | jun | c | | 2 | java | a | | 2 | eye | b | | 2 | com | c | +------+------+------+ 6 rows in set (0.00 sec)
mysql> select aa.id ,aa.name , bb.name,cc.name -> from (select id, name from teamtest where type='a') aa , -> (select id , name from teamtest where type ='b') bb, -> (select id , name from teamtest where type ='c') cc -> where aa.id = bb.id and bb.id =cc.id; +------+------+------+------+ | id | name | name | name | +------+------+------+------+ | 1 | mao | da | jun | | 2 | java | eye | com | +------+------+------+------+
select dd.id ,aa.name , bb.name,cc.name
from (select id, name from teamtest where type='a') aa ,
(select id , name from teamtest where type ='b') bb,
(select id , name from teamtest where type ='c') cc,
(select id from teamtest group by id ) dd ,
dd left jion in aa
where dd.id = aa.id or dd.id = bb.id or dd.id = cc.id
需求就是需求, 给它完成了就可以了。 不要再想深了。 再有改就再变。 我本不想用sql来作这件事的, 但这种变化是时时的。 得用job来作不能用java定时器。。。。所以。。。
我遇到过这样的需求,需要对查询结果进行转置,不知道楼主有没有好的办法,貌似和上面的这个问题差不多...
:arrow: 不明白,这个sql为了怎样的需求而实现的,表里id值为2的name值永远都只有java,eye,com吗,不会再出现cn?不解...