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. django - 后臺返回的json數據經過Base64加密,獲取時用python如何解密~!2. node.js - node 客戶端socket一直報錯Error: read ECONNRESET,用php的socket沒問題哈。。3. tp6表單令牌4. 網絡傳輸協議 - 以下三種下載方式有什么不同?如何用python模擬下載器下載?5. docker 17.03 怎么配置 registry mirror ?6. 我的html頁面一提交,網頁便顯示出了我的php代碼,求問是什么原因?7. 數據庫 - 使用讀寫分離后, MySQL主從復制延遲會導致讀不到數據嗎?8. angular.js - Angular路由和express路由的組合使用問題9. java 排序的問題10. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?
