mysql group中能否使用兩個count呢
問題描述
問題解答
回答1:其實最好寫明你的表結構,以下答案基于你提供的有限信息:
select district as 行政區(qū),count(1) as 小區(qū)數(shù) -- 我默認你每個小區(qū)時一條記錄,且無重復, sum(if(idNB = 1 ,1 ,0)) as 高檔小區(qū)數(shù) -- 假設高檔小區(qū)的idNB標記為1from table_name group by district其實 sum(if(idNB = 1 ,1 ,0)) 也可以替換成count(idNB = 1 or null)回答2:
mysql不支持分析函數(shù):
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
分析函數(shù)的寫法:
select district, count(xiaoqu) over (district) count_xiaoqu, count(idNB) over (district) count_idNBfrom table回答3:
我這邊說下我的思路吧,使用MySQL將區(qū)內的高端小區(qū)和非高端小區(qū)統(tǒng)計出來
select district,idNB,count(*) from xx GROUP BY district,idNB
然后區(qū)內小區(qū)的總數(shù)再由服務端這邊自己處理計算。
相關文章:
1. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?2. mysql - ubuntu開啟3306端口失敗,有什么辦法可以解決?3. javascript - 從mysql獲取json數(shù)據(jù),前端怎么處理轉換解析json類型4. android - 安卓做前端,PHP做后臺服務器 有什么需要注意的?5. mysql - C#連接數(shù)據(jù)庫時一直這一句出問題int i = cmd.ExecuteNonQuery();6. django - Python error: [Errno 99] Cannot assign requested address7. mysql replace 死鎖8. thinkPHP5中獲取數(shù)據(jù)庫數(shù)據(jù)后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙9. extra沒有加載出來10. javascript - 微信網(wǎng)頁開發(fā)從菜單進入頁面后,按返回鍵沒有關閉瀏覽器而是刷新當前頁面,求解決?
