SQLite MAX()函数

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

语法

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

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

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

示例1:

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

《SQLite MAX()函数》

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

SELECT MAX(FEES) AS "Highest Fees"  FROM STUDENT; 

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

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

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

示例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;

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

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>

        原文作者:SQLite教程
        原文地址: https://www.yiibai.com/sqlite/max.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞

    发表评论

    电子邮件地址不会被公开。 必填项已用*标注