mysql group中能否使用兩個(gè)count呢
問(wèn)題描述
問(wèn)題解答
回答1:其實(shí)最好寫(xiě)明你的表結(jié)構(gòu),以下答案基于你提供的有限信息:
select district as 行政區(qū),count(1) as 小區(qū)數(shù) -- 我默認(rèn)你每個(gè)小區(qū)時(shí)一條記錄,且無(wú)重復(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ù)的寫(xiě)法:
select district, count(xiaoqu) over (district) count_xiaoqu, count(idNB) over (district) count_idNBfrom table回答3:
我這邊說(shuō)下我的思路吧,使用MySQL將區(qū)內(nèi)的高端小區(qū)和非高端小區(qū)統(tǒng)計(jì)出來(lái)
select district,idNB,count(*) from xx GROUP BY district,idNB
然后區(qū)內(nèi)小區(qū)的總數(shù)再由服務(wù)端這邊自己處理計(jì)算。
相關(guān)文章:
1. mac里的docker如何命令行開(kāi)啟呢?2. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””3. nignx - docker內(nèi)nginx 80端口被占用4. docker容器呢SSH為什么連不通呢?5. 為什么我ping不通我的docker容器呢???6. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題7. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問(wèn)題。8. docker-compose 為何找不到配置文件?9. javascript - 最近用echarts做統(tǒng)計(jì)圖時(shí)遇到兩個(gè)問(wèn)題!!10. docker gitlab 如何git clone?

網(wǎng)公網(wǎng)安備