遇到一个需求,需要将数据库的字段,行转列
1.本来查询出来,是六行数据,现在要把六行数据,加在一行的,六列里面。这里需要用到行转列:
第一步:将查询出来的六行数据,做一个表,然后进行关联,在把每一个值取出来,放进新的列里面。
(select distinct m.accountId, (select n.saveEndCount from (
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_1` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_2` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_3` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_4` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_5` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_6` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_7` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_8` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_9` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_10` where `status`!=1 group by accountId,skey )n
where m.accountId=n.accountId and n.skey='1' ) AS 'skye1',
(select n.saveEndCount from (
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_1` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_2` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_3` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_4` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_5` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_6` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_7` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_8` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_9` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_10` where `status`!=1 group by accountId,skey )n
where m.accountId=n.accountId and n.skey='2' ) AS 'skye2',
(select n.saveEndCount from (
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_1` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_2` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_3` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_4` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_5` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_6` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_7` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_8` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_9` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_10` where `status`!=1 group by accountId,skey )n
where m.accountId=n.accountId and n.skey='3' ) AS 'skye3',
(select n.saveEndCount from (
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_1` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_2` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_3` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_4` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_5` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_6` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_7` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_8` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_9` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_10` where `status`!=1 group by accountId,skey )n
where m.accountId=n.accountId and n.skey='4' ) AS 'skye4',
(select n.saveEndCount from (
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_1` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_2` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_3` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_4` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_5` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_6` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_7` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_8` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXX`.`cpn_2022_festival_my_fragment_9` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `XXXX`.`cpn_2022_festival_my_fragment_10` where `status`!=1 group by accountId,skey )n
where m.accountId=n.accountId and n.skey='5' ) AS 'skye5',
(select n.saveEndCount from (
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_1` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_2` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_3` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_4` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_5` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_6` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_7` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_8` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_9` where `status`!=1 group by accountId,skey
union all
select count(1) as saveEndCount,skey,accountId from `X`.`cpn_2022_festival_my_fragment_10` where `status`!=1 group by accountId,skey )n
where m.accountId=n.accountId and n.skey='6' ) AS 'skye6' from