Home > Database > Mysql Tutorial > body text

How to use MySQL's MIN function to find the smallest value in a data table

王林
Release: 2023-07-26 11:05:11
Original
1769 people have browsed it

How to use MySQL's MIN function to find the smallest value in the data table

In the MySQL database, we often need to query and calculate the data table under certain conditions. Finding the smallest value is a common requirement, and MySQL provides the MIN function to meet this requirement.

The syntax of the MIN function is as follows:

MIN(expression)
Copy after login

Here expression represents the field or expression that needs to calculate the minimum value. Below we use a specific example to demonstrate how to use the MIN function to find the smallest value in the data table.

Suppose we have a data table named students, which contains two fields: students' names and scores. As shown below:

+--------+-------+
|  姓名  |  分数 |
+--------+-------+
|  张三  |  80   |
|  李四  |  90   |
|  王五  |  70   |
|  小明  |  95   |
|  小红  |  85   |
+--------+-------+
Copy after login

Now our goal is to find the student with the lowest score. We can use the MIN function to achieve this goal. The specific query statement is as follows:

SELECT 姓名, MIN(分数) AS 最低分
FROM students;
Copy after login

Running the above query statement, we will get the following results:

+--------+-------+
|  姓名  | 最低分|
+--------+-------+
|  王五  |  70   |
+--------+-------+
Copy after login

As you can see, the minimum score is 70, and the corresponding student name is Wang Wu.

The above example only demonstrates the basic usage of the MIN function, and actual applications may involve more complex queries and calculations. Here's a slightly more complex example.

Suppose we have a data table named orders, which contains the order number, amount and order date of different orders. As shown below:

+--------+-------+------------+
| 订单号 | 金额  | 订单日期    |
+--------+-------+------------+
|   001  |  100  | 2020-01-01 |
|   002  |  150  | 2020-01-02 |
|   003  |  80   | 2020-01-03 |
|   004  |  200  | 2020-01-04 |
|   005  |  120  | 2020-01-05 |
+--------+-------+------------+
Copy after login

Now our goal is to find the order with the lowest amount and display the corresponding order number and order date. This can be achieved using the following query statement:

SELECT 订单号, 订单日期, MIN(金额) AS 最低金额
FROM orders;
Copy after login

Running the above query statement, we will get the following results:

+--------+------------+----------+
| 订单号 | 订单日期    | 最低金额 |
+--------+------------+----------+
|   003  | 2020-01-03 |   80     |
+--------+------------+----------+
Copy after login

As you can see, the minimum amount is 80, the corresponding order number is 003, and the order date for 2020-01-03.

Through the above two examples, we can see that using MySQL's MIN function can easily find the minimum value in the data table. According to actual needs, we can also combine other functions and conditions to perform more complex queries and calculations.

I hope this article will help you understand how to use MySQL's MIN function to find the smallest value in the data table. If you have any questions, please feel free to leave a message.

The above is the detailed content of How to use MySQL's MIN function to find the smallest value in a data table. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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!