数据库是MYSQL
test表的示意结构和内容如下:
type   count
1       3
2       2
2       3
1       20
1       53
2       13我如果要得到count < 10的各种type的记录数目,因此我用以下sql可得到结果
select count(type),type from test where count < 10 group by type
得到结果如下:
count(type) type
1           1
2           2同理:我也可以得到count >10的各种type的记录数目
select count(type),type from test where count > 10 group by type
得到结果如下:
count(type) type
2           1
1           2为了提高效率现在我希望用一次SQL查询过程,把上面两句的SQL查询结果一次性得到。
预期结果如下:
count(type) type
1           1    (注:count<10)
2           2    (注:count<10)
2           1    (注:count>10)
1           2    (注:count>10)谢谢

解决方案 »

  1.   

    select type,if(count<10,'count<10','count>10'),count(*)
    from test
    group by type,if(count<10,'count<10','count>10')
      

  2.   

    多谢高手。
    那我可不可以多问一些?
    如果count条件变成三个,如count > 10, 10<count<100, count>100.这个if里面该如何写?
      

  3.   

    哈哈,自己写出来了。谢谢高手给了一个思路select type,count(*),case 
    when count <10 then "<10"
    when count >=10 and count < 100 then "<10<100"
    when count >=100 then ">100" end
    AS count
    from test
    group by type,case 
    when count <10 then "<10"
    when (count >=10 and count < 100) then "<10<100"
    when count >=100 then ">100" end