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; sqlite>
示例2:
使用具有的GROUP BY子句的MIN()
函数:
从student
表中检索NAME
和MIN 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>