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. java servlet后臺導(dǎo)出上萬條數(shù)據(jù)到excel,太慢!求解2. javascript - 一排三個(gè)框,各個(gè)框的間距是15px,距離外面的白框間距也是15px,這個(gè)css怎么寫?3. mysql函數(shù)unix_timestamp如何處理1970.1.1以前的數(shù)據(jù)?4. 怎樣使留言的數(shù)據(jù)與登錄的用戶名,密碼保持一致(在數(shù)據(jù)庫上是一行的)。5. mysql money 插入數(shù)據(jù)為什么報(bào)錯?6. java - 關(guān)于使用POI解析excel中的時(shí)間格的值7. html - 急求,能否用flex或者高端點(diǎn)的CSS來布局這個(gè)圖呢?不想用浮動或定位了8. css如何實(shí)現(xiàn)兩欄布局,左邊固定寬度,右邊寬度自適應(yīng),且高度和瀏覽器當(dāng)前高度一致?9. javascript - vue-cli 發(fā)布之后,如何在接口地址中去掉在開發(fā)環(huán)境中設(shè)置的跨域proxyTable配置10. 請問永久和臨時(shí)重定向有什么區(qū)別
