表A(user)
Id username dept
1 User1 部门一
2 User2 部门一
3 User2 部门二表B(article)
Id type number userId
1 论文1 1 1
2 论文1 3 2
3 论文2 1 3
4 专著1 1 1
5 专著2 2 3
6 论文1 2 3查询结果
论文 专著
部门一 4 1
部门二 3 2根据表A 和表B ,得到查询结果,其中,论文只有论文1和论文2两种类型,专著只有专著1和专著2两种类型。表B的userId外键关联表A的Id.

解决方案 »

  1.   

    select B.type,group_concat(B.number separator ' ')
    from A,B
    where A.id = B.userid
    group by B.type
      

  2.   

    SELECT a.dept,b.type,sum(number) from user a left join article b on a.id=b.userid group by a.dept,b.type对此结果再SUM(IF())即可
      

  3.   

    select dept,
    sum(if(left(b.type,2)='论文',1,0)) as 论文,
    sum(if(left(b.type,2)='专著',1,0)) as 专著
    from user a inner join article b on a.Id=b.userId
    group by dept