mysql group中能否使用两个count呢
PHP中文网
PHP中文网 2017-04-17 15:38:08
0
3
719
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
黄舟

其实最好写明你的表结构,以下答案基于你提供的有限信息:

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)
阿神

mysql不支持分析函数:

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

分析函数的写法:

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

我这边说下我的思路吧,使用MySQL将区内的高端小区和非高端小区统计出来

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

然后区内小区的总数再由服务端这边自己处理计算。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!