mysql group中能否使用两个count呢
问题描述
问题解答
回答1:其实最好写明你的表结构,以下答案基于你提供的有限信息:
select district as 行政区,count(1) as 小区数 -- 我默认你每个小区时一条记录,且无重复, sum(if(idNB = 1 ,1 ,0)) as 高档小区数 -- 假设高档小区的idNB标记为1from table_name group by district其实 sum(if(idNB = 1 ,1 ,0)) 也可以替换成count(idNB = 1 or null)回答2:
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_idNBfrom table t1
分析函数的写法:
select district, count(xiaoqu) over (district) count_xiaoqu, count(idNB) over (district) count_idNBfrom table回答3:
我这边说下我的思路吧,使用MySQL将区内的高端小区和非高端小区统计出来
select district,idNB,count(*) from xx GROUP BY district,idNB
然后区内小区的总数再由服务端这边自己处理计算。
相关文章:
1. linux - python 抓取公众号文章遇到验证问题2. docker 17.03 怎么配置 registry mirror ?3. javascript - 微信公号里采用七牛上传视频部分手机不能选择文件4. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””5. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?6. MySQL 如何从 datetime 类型的字段选择 date7. angular.js - angular中的a标签不起作用8. python - 怎样在linux下开发flask web应用时查看代码出错(traceback)的地方。9. sublime-text - sublime text 3中编译python(sublimeREPL),如何仅运行单行/选定部分10. PHP怎么升级?升级会导致老的网站程序不能用吗?

网公网安备