# `GROUP BY` 分组

__*聚合函数的作用*__
__*GROUP BY 操作*__
__*ONLY_FULL_GROUP_BY SQL 模式*__

+ 将行记录分组
+ `ONLY_FULL_GROUP_BY` SQL 模式
  + 如果出现在 SELECT 列表中的列, 并没有在 GROUP BY 或是在聚合函数中出现, 此 SQL 语句不合法
```8
tidb> SELECT
    -> ring_systems, 
    -> count(name), sum(mass)
    -> FROM universe.planets
    -> GROUP BY ring_systems;
+--------------+-------------+--------------------+
| ring_systems | count(name) | sum(mass)          |
+--------------+-------------+--------------------+
|            0 |           6 | 19.406899828463793 |
|            1 |           4 |  2654.800003051758 |
+--------------+-------------+--------------------+
2 rows in set (0.00 sec)

tidb> SELECT ring_systems, ~!@category_id@!~, count(*) FROM universe.planets GROUP BY ring_systems;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'universe.planets.category_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
```
-----------------------------------------------------------

```
tidb> SELECT ring_systems, category_id, count(*) FROM universe.planets GROUP BY ring_systems;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'universe.planets.category_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

tidb> select @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```