name   type(a/b/c)
1012       a
1012       b
1013       a
1013       a
1014       c
求一个sql语句查出每个name对应的type的a类有多少个,b类有多少,c类有多少

解决方案 »

  1.   

    试试下面的语句:select 
      name, 
      sum(if(type = 'a', 1, 0)) as 'a',
      sum(if(type = 'b', 1, 0)) as 'b',
      sum(if(type = 'c', 1, 0)) as 'c',
    from 你的表
    group by name
      

  2.   

    上面多了个逗号……:select
     name,
     sum(if(type = 'a', 1, 0)) as 'a',
     sum(if(type = 'b', 1, 0)) as 'b',
     sum(if(type = 'c', 1, 0)) as 'c'
    from 你的表 group by name
      

  3.   


    select name as a,type as b,(select count(*) from test2 where name=a and type=b) from test2 group by name,type
      

  4.   


    select name ,type ,(select count(*) from test2 where name=a.name and type=a.type) from test2 a group by name,type
      

  5.   

    select name,type,count(*)
    from tb 
    group by name,type