SQLite MAX()函数

编辑时间: 2018-02-07 12:52:30    关键字:

 SQLite MAX()函数用于获取表达式或给定列的最大值。

语法

SELECT MAX(aggregate_expression)   FROM tables   [WHERE conditions]; 
SQL

GROUP BY子句中使用Max()函数时的语法:

SELECT expression1, expression2, ... expression_n   MAX(aggregate_expression)   FROM tables   [WHERE conditions]   GROUP BY expression1, expression2, ... expression_n; 
SQL

示例1:

假设有一个名为student的表,具有以下数据:

student表中检索学生的最高费用(fees):

SELECT MAX(FEES) AS "Highest Fees"  FROM STUDENT;   -- 最大年龄 SELECT MAX(age) AS "Highest age"  FROM STUDENT; 
SQL

执行上面查询代码,得到以下结果 -

sqlite> SELECT MAX(FEES) AS "Highest Fees"  FROM STUDENT; 65000.0 sqlite> sqlite> -- 最大年龄 sqlite> SELECT MAX(age) AS "Highest age"  FROM STUDENT; 27 sqlite> 
SQL

示例2:

使用具有的GROUP BY子句的MAX()函数:

student表中检索NAME,ADDRESSMAX(FEES),并按ADDRESS的数据排序:

SELECT NAME, ADDRESS, MAX(FEES) AS "Highest FEES"   FROM STUDENT   WHERE ID <= 8   GROUP BY ADDRESS; 
SQL

执行上面代码,得到以下结果 -

sqlite> SELECT NAME, ADDRESS, MAX(FEES) AS "Highest FEES"    ...> FROM STUDENT    ...> WHERE ID <= 8    ...> GROUP BY ADDRESS; Minsu|Beijing|15000.0 Linsu|Guangzhou|65000.0 Sqlsu|Haikou|25000.0 Avgsu|Shanghai|2000.0 Maxsu|Shengzhen|20000.0 sqlite> 
SQL

推荐热图

合作推荐

2010-2018 可思数据版权所有 About SYKV | ICP备案:京ICP备14056871号