mysql group中能否使用两个count呢
PHP中文网
PHP中文网 2017-04-17 15:38:08
0
3
709
PHP中文网
PHP中文网

认证高级PHP讲师

全部回覆(3)
黄舟

其實最好寫明你的表格結構,以下答案是根據你提供的有限資訊:

select district as 行政区
,count(1) as 小区数 -- 我默认你每个小区时一条记录,且无重复
, sum(if(idNB = 1 ,1 ,0)) as 高档小区数 -- 假设高档小区的idNB标记为1
from table_name 
group by district

其实 sum(if(idNB = 1 ,1 ,0)) 也可以替换成count(idNB = 1 or null)
阿神

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_idNB
from table t1

分析函數的寫法:

select district, 
count(xiaoqu) over (district) count_xiaoqu, 
count(idNB) over (district) count_idNB
from table
巴扎黑

我這邊說下我的思路吧,使用MySQL將區內的高階小區和非高階小區統計出來

select district,idNB,count(*) from xx GROUP BY district,idNB

然後區內小區的總數再由服務端這邊自己處理計算。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!