Home >Database >Mysql Tutorial >Get minimum and maximum values ​​in MySQL

Get minimum and maximum values ​​in MySQL

PHPz
PHPzforward
2023-09-13 18:01:041471browse

在 MySQL 中获取最小值和最大值

We need to use MAX(columnName) to find the maximum value in a column and MIN(columnName) to find the maximum value in a column.

Suppose the following is the syntax to find the highest and lowest value in a specific column -

mysql> SELECT @min_val:=MIN(columnName),@max_val:=MAX(columnName) FROM tableName;
mysql> SELECT * FROM tableName WHERE columnName=@min_val OR columnName=@max_val;

Note: Suppose we have a database named "StudentsRecords" and a name The table for "STUDENT".

The following is our table -

##S00190S002 97##S003We will now write the query -
Student ID

Student ID

Student Score

72

Query

mysql> SELECT @min_val:=MIN(StudentMarks),@max_val:=MAX(StudentMarks) FROM STUDENT;
mysql> SELECT * FROM STUDENT WHERE StudentMarks =@min_val OR StudentMarks =@max_val;

Output

+---------------------+
| StudentMarks        |
+---------------------+
| 97                  |
+---------------------+

In the above query, "StudentMarks" refers to the name of the column. "STUDENT" is the name of the table from which the minimum and maximum values ​​are queried.

The above is the detailed content of Get minimum and maximum values ​​in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete