MySQL:类似Pivot的查询结果

时间:2017-03-09 18:42:47

标签: mysql

给出以下查询,该查询生成学生列表及其分配的同类群组:

SELECT mdl_user.lastname, mdl_user.firstname, mdl_cohort.name
FROM mdl_cohort
INNER JOIN mdl_cohort_members ON mdl_cohort_members.cohortid = mdl_cohort.id
INNER JOIN mdl_user ON mdl_cohort_members.userid = mdl_user.id
ORDER BY mdl_user.lastname, mdl_cohort.name

是否有可能产生一个结果,显示每个群组名称ONCE,最好是列名,每个学生分配给下面列出的同一群组?像这样:

Engineering Cohort    Administrative Cohort    IT Cohort
------------------    ---------------------    ---------
John Doe              Jane Smith               Jane Doe
Jane Smith            John Smith               John Doe
Dan Jones             Dana Jones               Dana Jones

2 个答案:

答案 0 :(得分:1)

这里有一些很好的例子,认为这可能会有所帮助: Pivot table basics: rows to columns

答案 1 :(得分:1)

这可以编写脚本..纯粹基于SQL的方法非常难看。以下方法将结果加载到表中(计数器从每个计数器增加一个)然后连接在一起。最后的工会模仿一个完整的外部联接,以确保不会留下任何队列。

set @admin:=0;
set @eng:=0;
set @it:=0;

drop table if exists administrative;
create table administrative as
      select (@admin := @admin + 1) as counter
          , cohort
       from (    select concat(mdl_user.firstname, ' ', mdl_user.lastname) as cohort
                   from mdl_cohort
              inner join mdl_cohort_members on (mdl_cohort_members.cohortid = mdl_cohort.id)
              inner join mdl_user on (mdl_cohort_members.userid = mdl_user.id)
                  where mdl_cohort.name='Administrative Cohort' 
               order by mdl_user.lastname, mdl_user.firstname) as admin;

drop table if exists engineering;
create table engineering as
      select (@eng := @eng + 1) as counter
          , cohort
       from (    select concat(mdl_user.firstname, ' ', mdl_user.lastname) as cohort
                   from mdl_cohort
             inner join mdl_cohort_members on (mdl_cohort_members.cohortid = mdl_cohort.id)
             inner join mdl_user on (mdl_cohort_members.userid = mdl_user.id)
                  where mdl_cohort.name='Engineering Cohort' 
               order by mdl_user.lastname, mdl_user.firstname) as eng;

drop table if exists it;
create table it as
      select (@it := @it + 1) as counter
           , cohort
        from (    select concat(mdl_user.firstname, ' ', mdl_user.lastname) as cohort
                    from mdl_cohort
              inner join mdl_cohort_members on (mdl_cohort_members.cohortid = mdl_cohort.id)
              inner join mdl_user on (mdl_cohort_members.userid = mdl_user.id)
                   where mdl_cohort.name='IT Cohort' 
                order by mdl_user.lastname, mdl_user.firstname) as it;

select admin.cohort as `Administrative Cohorts`,
       eng.cohort   as `Engineering Cohorts`,
       it.cohort    as `IT Cohorts`
  from administrative admin
 left join engineering eng on(eng.counter=admin.counter)
 left join it on (it.counter=admin.counter)
 union
select ''         as `Administrative Cohorts`,
       eng.cohort as `Engineering Cohorts`,
       it.cohort  as `IT Cohorts`
  from engineering eng
 left join it on (it.counter=eng.counter)
 where not exists (select 1
                     from administrative 
                   where counter=eng.counter)
union
select ''         as `Administrative Cohorts`,
       ''         as `Engineering Cohorts`,
       it.cohort  as `IT Cohorts`
  from it
 where not exists (select 1
                     from administrative
                    where counter=it.counter)
   and not exists (select 1
                     from engineering
                    where counter=it.counter);