SQLite COUNT函数
SQLite COUNT函数用于获取表达式的总数。
语法:
SELECT COUNT(aggregate_expression)
FROM tables
[WHERE conditions];
使用GROUP BY子句时与MAX函数一起使用的语法:
SELECT expression1, expression2, ... expression_n
COUNT(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;
示例1:
我们有一个名为“STUDENT”的表,其中包含以下数据:
计算“STUDENT”表中年龄大于22岁的学生数量。
SELECT COUNT(*) AS "Number of students"
FROM STUDENT
WHERE AGE > 22;
输出:
示例2:
使用GROUP BY和COUNT函数:
计算特定姓名的学生数量,其中年龄大于22,并按姓名对学生进行分组。
SELECT NAME, COUNT(*) AS "Number Of Students"
FROM STUDENT
WHERE AGE > 22
GROUP BY NAME;
输出: