MySQL 分組後,統計記錄條數

2022-06-17 05:36:08 字數 517 閱讀 8843

分組後,統計記錄條數:

select num,count(*) as counts from test_a group by num; 

查詢結果如下:

對num去重後的數量的統計:

select count(t.counts) from ( select num,count(*) as counts from test_a group by num ) as t;

select count(distinct num) as count from test_a;

它倆結果一樣,都是5;只是乙個是子查詢(巢狀),乙個是內建函式 distinct();

資料庫結構

sql 查詢分組後每組的總條數

使用者表 t basic user 外來鍵 unitsid 單位表 t units select count b.id u.fensuo from t basic user as b t units as u where b.unitsid u.id group by u.fensuo 此查詢用於 ...

mysql 區間分組統計

select elt interval c.money,0,1000,2000,3000,4000,5000 less 1000 1000to2000 2000to3000 3000to4000 4000to5000 as level,count c.id as cnt from select su...

Mysql橫向分組統計

有這麼一張表trade,記錄著毛巾和枕頭的交易資訊,表結構如圖a a 目前的資料如圖b b 現在要查詢出每天每個商品交易的總價,如下圖所示 c 那麼該如何查詢呢?方法不唯一,這裡先列舉了一種方法 select date format created time,y m d as 日期 sum if p...