Home > Database > Mysql Tutorial > body text

Get minimum and maximum values ​​in MySQL

PHPz
Release: 2023-09-13 18:01:04
forward
1404 people have browsed it

在 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;
Copy after login

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;
Copy after login

Output

+---------------------+
| StudentMarks        |
+---------------------+
| 97                  |
+---------------------+
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!