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
的表,具有以下資料:
從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
,ADDRESS
和MAX(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>