表 msg 
  id       type
  1         1
  2         2
  3         2
  4         1   
  5         2 
  6         1
  .         .
  .         . 
  .         .type 只有两种类型 1或者2 
求用 一条语句  查询出  type =1 和type =2 的个数是一条 谢谢

解决方案 »

  1.   

    select 
    TYPE,
    COUNT(*) as 个数
    from msg
    group by [type]
      

  2.   

    select 
     [type], 
    COUNT(*) as 个数 
    from msg 
    group by [type]  
      

  3.   

    select 
      sum(case when type=1 then 1 else 0 end) as [type=1的个数],
      sum(case when type=2 then 1 else 0 end) as [type=2的个数]
    from
      msg
      

  4.   

    select count(2-type) as type1, count(type-1) as type2 from type
      

  5.   

    select sum(2-type) as type1, sum(type-1) as type2 from type