更新时间:2024-11-11 GMT+08:00

GROUP BY

完整的分析语句语法如下:

SELECT [DISTINCT] (* | expression) [AS alias] [, ...]
[GROUP BY expression [, ...] [HAVING predicates]]
[ORDER BY expression [ASC | DESC] [, ...]]
[LIMIT size OFFSET offset]

其中,GROUP BY表示按值分组。本章节内容将介绍GROUP BY语法参数说明及示例。

按字段的值分组

SELECT age GROUP BY age
表1 按字段的值分组

age

28

32

36

按字段别名分组

SELECT account_number AS num GROUP BY num
表2 按字段别名分组

num

1

16

13

18

按多个字段分组

SELECT account_number AS num, age GROUP BY num, age
表3 按多个字段分组

num

age

1

32

16

36

13

28

18

32

使用SQL函数

函数相关内容请参见函数

SELECT LENGTH(lastname) AS len, COUNT(*) AS count GROUP BY LENGTH(lastname)
表4 使用SQL函数

len

count

4

2

5

2