SQLite MIN()函式


SQLite MIN()函式用於獲取表示式或給定列的最小值。

語法

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

GROUP BY子句中使用Min()函式時的語法:

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

範例1:

假設有一個名為student的表,具有以下資料:

student表中檢索學生的最低費用(fees):

SELECT MIN(FEES) AS "Lowest Fees"  FROM STUDENT; 

-- 最小年齡
SELECT MIN(age) AS "Lowest age"  FROM STUDENT;

執行上面查詢程式碼,得到以下結果 -

sqlite> SELECT MIN(FEES) AS "Lowest Fees"  FROM STUDENT;
2000.0
sqlite>
sqlite> SELECT MIN(FEES) AS "Lowest Fees"  FROM STUDENT;
2000.0
sqlite>
sqlite> -- 最小年齡
sqlite> SELECT MIN(age) AS "Lowest age"  FROM STUDENT;
21
sqlite>

範例2:

使用具有的GROUP BY子句的MIN()函式:

student表中檢索NAMEMIN FEES,並按NAME的資料排序:

SELECT NAME, MIN(FEES) AS "Lowest Fees"  
FROM STUDENT  
WHERE ID <= 8  
GROUP BY NAME;

執行上面程式碼,得到以下結果 -

sqlite> SELECT NAME, MIN(FEES) AS "Lowest Fees"
   ...> FROM STUDENT
   ...> WHERE ID <= 8
   ...> GROUP BY NAME;
Avgsu|2000.0
Javasu|18000.0
Linsu|10000.0
Maxsu|20000.0
Minsu|5000.0
Sqlsu|25000.0
sqlite>