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. angular.js - 不适用其他构建工具,怎么搭建angular1项目2. python如何不改动文件的情况下修改文件的 修改日期3. mysql - 一个表和多个表是多对多的关系,该怎么设计4. javascript - git clone 下来的项目 想在本地运行 npm run install 报错5. mysql主从 - 请教下mysql 主动-被动模式的双主配置 和 主从配置在应用上有什么区别?6. android-studio - Android 动态壁纸LayoutParams问题7. 主从备份 - 跪求mysql 高可用主从方案8. angular.js - 三大框架react、vue、angular的分析9. python 如何实现PHP替换图片 链接10. python - django 里自定义的 login 方法,如何使用 login_required()
