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. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。2. javascript - [js]為什么畫布里不出現圖片呢?在線等3. python - 如何判斷爬蟲已經成功登陸?4. html - vue項目中用到了elementUI問題5. html5 - 有可以一次性把所有 css外部樣式轉為html標簽內style=" "的方法嗎?6. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變7. javascript - 原生canvas中如何獲取到觸摸事件的canvas內坐標?8. javascript - 有什么比較好的網頁版shell前端組件?9. javascript - 這不是對象字面量函數嗎?為什么要new初始化?10. javascript - 求解答:實例對象調用constructor,此時constructor內的this的指向?
