Home  >  Q&A  >  body text

mysql group中能否使用两个count呢

PHP中文网PHP中文网2743 days ago716

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 15:40:08

    In fact, it is best to describe your table structure. The following answer is based on the limited information you provided:

    select district as 行政区
    ,count(1) as 小区数 -- 我默认你每个小区时一条记录,且无重复
    , sum(if(idNB = 1 ,1 ,0)) as 高档小区数 -- 假设高档小区的idNB标记为1
    from table_name 
    group by district
    
    其实 sum(if(idNB = 1 ,1 ,0)) 也可以替换成count(idNB = 1 or null)

    reply
    0
  • 阿神

    阿神2017-04-17 15:40:08

    mysql does not support analytic functions:

    select t1.district, 
        (select count(t2.xiaoqu) from table t2 where t2.district=t1.district) count_xiaoqu,
        (select count(t2.idNB) from table t2 where t2.district=t1.district) count_idNB
    from table t1
    

    How to write analysis function:

    select district, 
    count(xiaoqu) over (district) count_xiaoqu, 
    count(idNB) over (district) count_idNB
    from table

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:40:08

    Let me tell you my thoughts here. Use MySQL to count the high-end communities and non-high-end communities in the area

    select district,idNB,count(*) from xx GROUP BY district,idNB

    Then the total number of cells in the area is processed and calculated by the server itself.

    reply
    0
  • Cancelreply