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. css3 - 微信前端页面遇到的transition过渡动画的bug2. 网页爬虫 - 关于Python的编码与解码问题3. html5上的ICON一般去哪里找?4. css3 - 微信小程序如何把radio改成2个选择按钮的样式5. javascript - 为什么var obj = {}创建对象的方法里面不能用this.xxx来声明属性 ?6. javascript - Storage中removeItem在什么情况下使用7. javascript - postcss-loader在webpack2的使用.8. selenium-selenium-webdriver - python 将当前目录加入到 环境变量9. javascript - vue 父子组件传递数据10. css - input间的间距和文字上下居中

网公网安备