mysql行转移到列后如何在mybatis中显示结果?

时间:2018-07-26 05:16:49

标签: mysql mybatis

这是我的代码,我使用一个过程将行转移到mysql中的列:

我的查询结果,之前:

name        studentnum  plannum  orgname  submitnum 
2018 second   24          56     QINGBEI      0
2019 second   0           0      Westen       0
2020 second   0           1      Gansu        0
2021 second   2           2      Shanxi       0
2022 second   3           3      Qinghai      0
2023 second   4           4      HEI          0
2024 second   5           5      SHIDAOWAN    0
2025 second   6           6      ZHE          0
2026 second   7           7      HUBEI        0

之后:

name    studentnum  plannum Westen  Gansu   Shanxi  Qinghai SHIDAOWAN    LI 
2018    24           56       0      4       0       0         0          3         

因此,首先,创建一个临时表来存储一些结果:

drop TEMPORARY TABLE if exists tmp_trainclassEnrolluser_statistic;
CREATE TEMPORARY TABLE  tmp_trainclassEnrolluser_statistic ENGINE = MEMORY 
    select
    tms.trainclass_id,tms.name,tms.sponsor_id,tms.undertake_id,
    (select orgname from torg where org_id=tms.sponsor_id ) sponorname, 
    (select orgname from torg where org_id=tms.undertake_id )  undertakename,                    
    a.plannum,b.totalsubmitnum,c.org_id,c.limitnum,d.submitorg_id,d.submitnum
   ,(select 
   IFNULL(orgabbr,SUBSTR(REPLACE(orgname,'suborg',''),1,2)) from torg where 
   org_id=c.org_id)  orgname 
   ,(select 
    IFNULL(orgabbr,SUBSTR(REPLACE(orgname,'suborg',''),1,2)) from torg where 
    org_id=d.submitorg_id)  submitorgname 
    from tms_trainclass tms
                left join (
                SELECT sum(enrsco.LIMITNUM) plannum,enr.RELATIVE_ID
                FROM ENROLL_ENROLL enr
                join ENROLL_SCOPE enrsco on enrsco.ENROLL_ID = enr.ENROLL_ID 
                WHERE  enr.TYPE = 2 
                group by enr.RELATIVE_ID
                ) a on a.RELATIVE_ID = tms.trainclass_id
                left join (
                SELECT  
                count(DISTINCT enr.USER_ID) totalsubmitnum ,en.RELATIVE_ID
                FROM ENROLL_USER enr
                join ENROLL_ENROLL en on  en.ENROLL_ID = enr.ENROLL_ID
                WHERE   enr.STATUS IN (1,3)
                group by en.RELATIVE_ID
            ) b on b.RELATIVE_ID = tms.trainclass_id
            left join (
                    SELECT  
                     enr.ENTITY_ID org_id
                    ,enr.limitnum,en.RELATIVE_ID
                    FROM ENROLL_SCOPE enr
                    join ENROLL_ENROLL en on  en.ENROLL_ID = enr.ENROLL_ID
                    group by en.RELATIVE_ID
            ) c on c.RELATIVE_ID = tms.trainclass_id
            left join (
                    SELECT  
                    enr.createdomain_id submitorg_id
                    ,count(DISTINCT enr.USER_ID) submitnum ,en.RELATIVE_ID
                    FROM ENROLL_USER enr
                    join ENROLL_ENROLL en on  en.ENROLL_ID = enr.ENROLL_ID
                    WHERE   enr.STATUS IN (1,3)
                    group by en.RELATIVE_ID
            ) d on d.RELATIVE_ID = tms.trainclass_id                
                group by tms.trainclass_id
                order by c.org_id

然后,我创建一个过程来传输上述结果:

DELIMITER &&  
drop procedure if exists p_trainclassenrolluser_report;
Create Procedure p_trainclassenrolluser_report(IN pageNumber 
DECIMAL(20),pageSize DECIMAL(20))
READS SQL DATA 
BEGIN
SET @sql = NULL;
SET @pageNumber = NULL;
SET @pageSize = NULL;
set @pageNumber = pageNumber*(pageNumber -1);
set @pageSize = pageSize;  set @rowindex = 0;

SELECT
GROUP_CONCAT(DISTINCT CONCAT(
'MAX(IF(s.trainclass_id=''',v.trainclass_id,''',s.limitnum,0)) AS ''', 
v.orgname, ''',
    MAX(IF(s.trainclass_id=''',v.trainclass_id,''',s.submitnum,0)) AS ''', 
v.submitorgname, '''' )

) 
INTO @sql
FROM tmp_trainclassEnrolluser_statistic v;

SET @sql = CONCAT('Select s.trainclass_id,s.name,s.sponorname,s.undertakename
,s.plannum,s.totalsubmitnum,s. 
org_id,s.submitorg_id, '
, @sql
,  'From tmp_trainclassEnrolluser_statistic  s  Group by s.trainclass_id 
limit ',@pageNumber,',',@pageSize);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END &&  
DELIMITER ;

CALL p_trainclassenrolluser_report(1,10); 

这是我的结果:

name    studentnum  plannum Westen  Gansu   Shanxi  Qinghai SHIDAOWAN    LI 
2018    24           56       0      4       0       0         0          3                             

如何将这些列(西方,甘肃,山西,青海,狮岛,李)输出到mybatis中?

mybatis结果集映射实体类:

<resultMap id="RM_Trainclass_rpt" type="Trainclass">
    <result property="name" column="NAME" jdbcType="VARCHAR" javaType="String"/>
    <result property="studentnum" column="studentnum" jdbcType="decimal"
        javaType="Integer"/>
    <result property="plannum" column="plannum" jdbcType="decimal" 
        javaType="Integer"/>
    ...
</resultMap>

0 个答案:

没有答案