有一个table(A,B,C)内容如下:A     B        C
湖北   武汉     武昌区
湖南   长沙     长沙区
湖北   武汉     武昌区
湖南   岳阳     岳阳区
海南   海口     秀英区
湖北   武汉     汉阳区
湖北   武汉     汉口区
海南   海口     龙华区
海南   三亚     河东区
------------------------如何统计A中相同字段数量,其中B中相同字段数量,C中相同数量

解决方案 »

  1.   

    没看明白你要是结果是什么
    给你一个SQL做参考
    select A,count(A) from tablename group by A;
    结果是
    湖北 4
    海南 3
    湖南 2不知道你要的具体效果是什么
      

  2.   

    我想要的结果是分项统计,分别统计A中相同的有多个,在A相同的字段中有多少个为B中相同的或有多少个为C中相同的例子如:
    湖北 4 其中武汉4个 武昌区有2个
    海南 3 其中海口2个
      

  3.   

    select a,count(*) from tt group by a
    union
    select a&b,count(*) from tt group by a&b
    union
    select a&b&c,count(*) from tt group by a&b&c
      

  4.   

    select a,count(*) from tt group by a
    union
    select concat(a,b),count(*) from tt group by concat(a,b)
    union
    select concat(a,b,c),count(*) from tt group by concat(a,b,c)
      

  5.   

    6楼的结果是计算a列的项及其合计,为了更好的表述的
    我的要求,我重新写了结构表tb(col1,col2,col3)如下:col1   col2    col3a        -1      -4
    a        3      -3
    b        0      -5
    a        1      6
    a        -2      -3
    b        1      1
    c        3      5
    a        -1      -4
    c        3      -3
    a        0      -5
    c        1      6
    a        -2      -3
    b        1      1
    a        3      5我想要的结果是:
      col1  col2>0    col3>0a   ?      ?         ?
    b   ?      ?         ?
    c   ?      ?         ?
      

  6.   

    select count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from tt group by col1
      

  7.   


     col1  col2>0    col3>0 a  8      3        2 
    b  3      2        2 
    c  3      3        2
      

  8.   

    谢谢wwwwb,但我还不明白sum(if(col3>0,1,0)) 语句的意思
      

  9.   

    简单地说,如COL3>0,则值为1,按COL1分组,累加值为1的个数
      

  10.   

    select count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from tt group by col1unionselect count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from tt group by 1
      

  11.   

    select count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from tt group by col1union allselect count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from tt group by 1
      

  12.   

    我加上 union all 运行出错
      

  13.   

    不会吧,我在MYSQL下调试通过
    你的代码 
      

  14.   

    呵呵,修改了一下:
    select count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from ttd group by col1
    union all
    select count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from ttd 
      

  15.   

    --可以使用with rollup来添加合计项.如下:select col1,
           count(*) as newclo1,
           sum(if(col2>0,1,0)) as newclo2,
           sum(if(col3>0,1,0)) as newclo3 
    from tb 
    group by col1 with rollup;
      

  16.   

    呵呵,用with rollup更简单一些,我的方法是传统的方法
      

  17.   

    我在php上显示出错"Parse error: syntax error, unexpected T_VARIABLE in tj.php on line 182"是不是在col1项出问题了?如何把最后一个改为"合计"
      

  18.   

    mysql没有GROUPING函数,无法判断哪一行是由ROLLUP生成的,要加"合计",估计要用UNION方法即传统的方法
      

  19.   

    修改了一下:
    select if(isnull(col1),'合计',col1),count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from ttd group by col1 with rollup
      

  20.   

    or
    select nullif(col1,'合计'),count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from ttd group by col1 with rollup
      

  21.   

    函数记错了
    select ifnull(col1,'合计'),count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from ttd group by col1 with rollup
      

  22.   

    WWWWA 我现在碰到一个问题,想要的结果是:
     col1  (col2==-1 or col==3)    col3>0 a  ?           ?                 ? 
    b  ?           ?                 ? 
    c  ?           ?                 ? select ifnull(col1,'合计'),count(*) as newclo1,sum(if(col2==-1 or col2==-1),1,0)) as newclo2, 
    sum(if(col3>0,1,0)) as newclo3 
    from ttd group by col1 with rollup提示出错!
      

  23.   

    不好意思有个错误,重新更正一下: 
    col1  (col2==-1 or col2==3)    col3>0 a  ?              ?                ? 
    b  ?              ?                ? 
    c  ?              ?                ? 
    select ifnull(col1,'合计'),count(*) as newclo1,sum(if(col2==-1 or col2==-1),1,0)) as newclo2, sum(if(col3>0,1,0)) as newclo3 
    from ttd group by col1 with rollup 提示出错!
      

  24.   

    select ifnull(col1,'合计'),count(*) as newclo1,
    sum(if(col2=-1 or col2=3,1,0)) as newclo2, sum(if(col3>0,1,0)) as newclo3
    from ttd group by col1 with rollupmysql中没有==吧
      

  25.   

    select ifnull(col1,'合计'),count(*) as newclo1,
    sum(if(col2=-1 or col2=3,1,0)) as newclo2, sum(if(col3>0,1,0)) as newclo3
    from ttd group by col1 with rollup 
      

  26.   

    select A,count(A), c,count(c), b,count(b) from tablename group by A,b,b; 
    a  count(a)  c  count(c)  b  count(b)  
    海南 1 河东区 1 三亚 1 
    海南 1 秀英区 1 海口 1 
    海南 1 龙华区 1 海口 1 
    湖北 2 武昌区 2 武汉 2 
    湖北 1 汉阳区 1 武汉 1 
    湖南 1 岳阳区 1 岳阳 1 
    湖南 1 长沙区 1 长沙 1 
      

  27.   

    我在mysql5中使用with rollup 很正常,但在mysql3.2的linux服务器上加上with rollup后所有的内容不能统计,不知道是否因为版本太低的原因,用传统的方法如何统计"合计"项.
      

  28.   


    select count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from ttd group by col1
    union all
    select count(*) as newclo1,sum(if(col2>0,1,0)) as newclo2,
    sum(if(col3>0,1,0)) as newclo3
    from ttd 
      

  29.   

    我前面已试过wwwwA上面的语句,它出来的内容是二次相同的信息,晕!
      

  30.   

    统计并排序:select name, count(name) as count from table group by name order by count desc;http://hi.baidu.com/sai_ying/blog/item/3db702faaceb573b4e4aea01.html
    ----------
    name  contenta  12332
    a  3434
    a  234234
    b  232
    c  23423
    c  34得出结果:name counta  3
    c   2
    b   1