mysql group中能否使用兩個(gè)count呢
問題描述
問題解答
回答1:其實(shí)最好寫明你的表結(jié)構(gòu),以下答案基于你提供的有限信息:
select district as 行政區(qū),count(1) as 小區(qū)數(shù) -- 我默認(rèn)你每個(gè)小區(qū)時(shí)一條記錄,且無重復(fù), sum(if(idNB = 1 ,1 ,0)) as 高檔小區(qū)數(shù) -- 假設(shè)高檔小區(qū)的idNB標(biāo)記為1from table_name group by district其實(shí) 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ū)內(nèi)的高端小區(qū)和非高端小區(qū)統(tǒng)計(jì)出來
select district,idNB,count(*) from xx GROUP BY district,idNB
然后區(qū)內(nèi)小區(qū)的總數(shù)再由服務(wù)端這邊自己處理計(jì)算。
相關(guān)文章:
1. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?2. 大家好,請問在python腳本中怎么用virtualenv激活指定的環(huán)境?3. 網(wǎng)頁爬蟲 - 用Python3的requests庫模擬登陸B(tài)ilibili總是提示驗(yàn)證碼錯誤怎么辦?4. javascript - 關(guān)于audio標(biāo)簽暫停的問題5. android - QQ物聯(lián),視頻通話6. Matlab和Python編程相似嗎,有兩種都學(xué)過的人可以說說嗎7. javascript - 微信小程序封裝定位問題(封裝異步并可能多次請求)8. javascript - Web微信聊天輸入框解決方案9. mysql - 怎么讓 SELECT 1+null 等于 110. 請教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒有反應(yīng)
