一张jbxx表,有两个字段ID,YBBH 业务类型编号,00代表美容,12代表美发,13代表养生,31代表化妆品,99代表测试,即:
表信息:
YBBH ID
00    1
12    2
13    3
31    4
99    5
12    6
13    7
99    8
13    9我想把每一种业务的个数求出来,还要把所有的个数进行累加
我这样写
SELECT count(YBBH),YBBH FROM jbxx group by YBBH 只能求出每一种业务的个数,但不能实现累加,请高手帮解决一下

解决方案 »

  1.   

    还要把所有的个数进行累加:所有记录数?用上述数据 ,结果是9?SELECT count(YBBH),YBBH,(select count(*) from jbxx) FROM jbxx group by YBBH
      

  2.   

    mysql> select ifnull(ybbh,'total'),count(1) from jbxx group by ybbh with rollup;
    +----------------------+----------+
    | ifnull(ybbh,'total') | count(1) |
    +----------------------+----------+
    | 00                   |        1 |
    | 12                   |        2 |
    | 13                   |        3 |
    | 31                   |        1 |
    | 99                   |        2 |
    | total                |        9 |
    +----------------------+----------+
    6 rows in set (0.00 sec)
      

  3.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  4.   


    很抱歉,因为时间匆忙,而且实际的表结构和数据都很复杂,所以就只能用简单的的汉语表达,不过以后提问我会注意这一点的,尽可能的表述清楚,让大家都来帮忙解答,谢谢!另外补充一点,就是3楼就是我想要的答案,不过因为之前都是使用的是MS SQL Server2005,现在项目需要使用MYSQL,本人对MYSQL的语法不是太清楚,所以会经常犯错误
      

  5.   

    你要的是这个?SELECT count(YBBH),YBBH FROM jbxx group by YBBH
    union all
    select count(*),'Total' from jbxx
      

  6.   


    对,就是这个效果,这个应该是联合查询吧,不过我都没用过 union all呢
      

  7.   

    select ifnull(ybbh,'total'),count(1) from jbxx group by ybbh with rollup;
    简洁一些
      

  8.   

    是简洁,不过我不理解ifnull,count(1),with rollup是什么意思啊
      

  9.   

    count(1)=count(*)ifnull:
    If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used. with rollup:
    The GROUP BY clause allows a WITH ROLLUP modifier that causes extra rows to be added to the summary output. These rows represent higher-level (or super-aggregate) summary operations. ROLLUP thus allows you to answer questions at multiple levels of analysis with a single query. It can be used, for example, to provide support for OLAP (Online Analytical Processing) operations