Home >Database >Mysql Tutorial >How to query the minimum value in mysql

How to query the minimum value in mysql

青灯夜游
青灯夜游Original
2022-06-27 14:46:458317browse

In mysql, you can use the SELECT statement to query all the data in the specified table, and then use the MIN() function to return the minimum value in the query result set. The query syntax is "SELECT MIN (specified column name) FROM table name;". The MIN() function is used to find the minimum value record in the result set. It can return the minimum value in the query column. You only need to pass the column name to be queried as a parameter to the MIN() function. The syntax "MAX(column name)" )".

How to query the minimum value in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

In mysql, you can use the SELECT statement and MIN() function to query the minimum value.

  • First use the SELECT statement to query all the data in the specified table

  • Then use the MIN() function to return the specified column in the query result set The minimum value is

SELECT MIN(指定列名) FROM 表名;

The MIN() function is used to find the minimum value record in the result set. It can return the minimum value in the query column; the accepted parameter is the specified column name.

Example of querying the minimum value:

Create an employee_tbl table with the following records:

mysql> SELECT * FROM employee_tbl;
+------+------+------------+--------------------+
| id   | name | work_date  | daily_typing_pages |
+------+------+------------+--------------------+
|    1 | John | 2007-01-24 |                250 |
|    2 | Ram  | 2007-05-27 |                220 |
|    3 | Jack | 2007-05-06 |                170 |
|    3 | Jack | 2007-04-06 |                100 |
|    4 | Jill | 2007-04-06 |                220 |
|    5 | Zara | 2007-06-06 |                300 |
|    5 | Zara | 2007-02-06 |                350 |
+------+------+------------+--------------------+
7 rows in set (0.00 sec)

Now suppose that based on the above table, you want To get the minimum value of daily_typing_pages, you can simply use the following command:

mysql> SELECT MIN(daily_typing_pages)
    -> FROM employee_tbl;
+-------------------------+
| MIN(daily_typing_pages) |
+-------------------------+
|                     100 |
+-------------------------+
1 row in set (0.00 sec)

Use the GROUP BY clause to find all the records with the minimum value of each name as follows:

mysql>SELECT id, name, MIN(daily_typing_pages)
    -> FROM employee_tbl GROUP BY name;
+------+------+-------------------------+
| id   | name | MIN(daily_typing_pages) |
+------+------+-------------------------+
|    3 | Jack |                     100 |
|    4 | Jill |                     220 |
|    1 | John |                     250 |
|    2 | Ram  |                     220 |
|    5 | Zara |                     300 |
+------+------+-------------------------+

5 rows In set (0.00 sec) you can use the Min() function and the Max() function together to find the minimum and maximum values. Try the following example:

mysql> SELECT MIN(daily_typing_pages) min, MAX(daily_typing_pages) max
    -> FROM employee_tbl;
+-------+------+
|  min  | max  |
+-------+------+
|   100 |  350 |
+-------+------+
1 row in set (0.01 sec)

[Related recommendations: mysql video tutorial]

The above is the detailed content of How to query the minimum value in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:what is mysql hintNext article:what is mysql hint