1 數據表數據
2 sum
- 功能
數據求和統計.
序號 | 表達式 | 描述 |
---|---|---|
1 | sum(column_name) | 所有列的列值相加 |
2 | sum(expression) | 統計滿足表達式的所有行 |
2.1 sum(column_name)
- 查詢語句:
select sum(id) from userinfos group by sex;
- 查詢結果:
+---------+
| sum(id) |
+---------+
| 166 |
| 36 |
+---------+
2.2 sum(experssion)
- 查詢語句:
select sum(position="CEO") from userinfos group by sex;
- 查詢結果:
+---------------------+
| sum(position="CEO") |
+---------------------+
| 4 |
| 1 |
+---------------------+
3 count
- 功能
數據數量統計.
3.1 數據形式
- 不存在NULL:
- 存在NULL:
3.2 count功能分類
序號 | 表達式 | 描述 |
---|---|---|
1 | count(*) | 統計所有行,包括NULL |
2 | count(column_name) | 統計列不爲NULL的行數 |
3 | count(expression) | 只要非NULL即+1,與條件表達式(expression)無關 |
3.2.1 count(*)
- 查詢語句:
select count(*) from userinfos group by sex;
- 查詢結果:
+----------+
| count(*) |
+----------+
| 11 |
| 5 |
+----------+
3.2.2 count(column_name)
position不存在null值:
- 查詢語句:
select count(position) from userinfos group by sex;
- 查詢結果:
+-----------------+
| count(position) |
+-----------------+
| 11 |
| 5 |
+-----------------+
position存在null值:
- 查詢語句:
select count(position) from userinfos group by sex;
- 查詢結果:
+-----------------+
| count(position) |
+-----------------+
| 10 |
| 5 |
+-----------------+
3.2.3 count(expression)
position不存在null值:
- 查詢語句:
select sex, count(position="CEO") from userinfos group by sex;
- 查詢結果:
+--------+-----------------------+
| sex | count(position="CEO") |
+--------+-----------------------+
| female | 11 |
| male | 5 |
+--------+-----------------------+
position存在null值:
- 查詢語句:
select sex, count(position="CEO") from userinfos group by sex;
- 查詢結果:
+--------+-----------------------+
| sex | count(position="CEO") |
+--------+-----------------------+
| female | 10 |
| male | 5 |
+--------+-----------------------+
4 found_rows
- 功能
獲取分組數據的行數.
4.1 一次查詢
- 查詢語句:
select found_rows() from userinfos group by sex;
- 查詢結果:
+--------------+
| found_rows() |
+--------------+
| 2 |
| 2 |
+--------------+
返回一個列表數據,兩行,每行數據爲分組數據的行數,讀取一行數據即可,若使用MyBatis,則統計列表的長度,因爲MyBatis中返回的數據爲1,1.
4.2 兩次查詢
- 查詢語句:
select sql_calc_found_rows sex from userinfos group by sex;
- 查詢結果:
+--------+
| sex |
+--------+
| female |
| male |
+--------+
查詢語句:
select found_rows();
查詢結果:
+--------------+
| found_rows() |
+--------------+
| 2 |
+--------------+
5 group by
- 功能
數據分組,細分兩個分類:單個分組字段,;多個分組字段,數據分組.
5.1 單字段分組
選擇(select)的對象,只能是分組的字段或進行統計的字段信息(如sum,count)
5.1.0 只獲取分組字段數據
- 查詢語句:
select sex from userinfos group by sex;
- 分組結果:
+--------+
| sex |
+--------+
| female |
| male |
+--------+
將sex字段進行分析,相同的內容進行合併,放在同一組中,返回一條數據.
5.1.2 獲取分組字段數據和統計信息
- 查詢語句:
select sex, count(*) from userinfos group by sex;
- 查詢結果:
+--------+----------+
| sex | count(*) |
+--------+----------+
| female | 11 |
| male | 5 |
+--------+----------+
對sex字段分析,並統計每組的所有數據數量,即female組中共有11個數據,male共有5個數據.
5.1.3 獲取統計信息
- 查詢語句:
select sex, count(*), sum(position="CEO") from userinfos group by sex;
- 查詢結果:
+--------+----------+---------------------+
| sex | count(*) | sum(position="CEO") |
+--------+----------+---------------------+
| female | 11 | 5 |
| male | 5 | 1 |
+--------+----------+---------------------+
使用sum獲取某個字段在分組中的數據,如position="CEO"的數據,female共有5個,male共有1個
5.1.4 無效語句
選擇(select)分組以外的字段,如(position),無法獲取數據.
- 查詢語句:
- 查詢結果:
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'data_repository.userinfos.position' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
選擇的列表(list[sex,position])不在分組語句中(GROUP BY)並且包含了非聚合的列position,該列不在分組中,無法獲取查詢結果,因此單個字段分組時,選擇的字段必須是分組的字段.
5.2 多字段分組
- 查詢語句:
mysql> select sex from userinfos group by sex, position;
- 查詢結果:
+--------+
| sex |
+--------+
| female |
| female |
| female |
| female |
| male |
| male |
| male |
| male |
+--------+
對sex和position字段進行過濾,先將sex字段分組,然後將position字段分析,合併查詢結果,選擇的對象爲分組對象的子集.
- 查詢語句:
select sex, position from userinfos group by sex, position;
- 查詢結果:
+--------+----------+
| sex | position |
+--------+----------+
| female | CEO |
| female | CFO |
| female | COO |
| female | CTO |
| male | CEO |
| male | CFO |
| male | COO |
| male | CTO |
+--------+----------+
先依據sex分組,後按照position分組,相同的合併.