A tutorial on using the Min () function in Mysql _mysql

Source: Internet
Author: User

The Min function of MySQL is used to find a record of the minimum value of a recordset.

To understand the Employee_tbl table that the Min function considers has the following records:

Mysql> SELECT * from EMPLOYEE_TBL;
+------+------+------------+--------------------+
| id |  name | work_date | daily_typing_pages
| +------+------+------------+--------------------+
|  1 | John | 2007-01-24 |        The |
|  2 | Ram | 2007-05-27 |        The |
|  3 | Jack | 2007-05-06 |        170 |
|  3 | Jack | 2007-04-06 |        |
|  4 | Jill | 2007-04-06 |        The |
|  5 | Zara | 2007-06-06 |        A |
|  5 | Zara | 2007-02-06 |        |
+------+------+------------+--------------------+
7 rows in Set (0.00 sec)

Now assume that the above table is based on the minimum value you want to take daily_typing_pages, so you can do this by simply using the following command:

Mysql> SELECT MIN (daily_typing_pages)
  -> from Employee_tbl;
+-------------------------+
| MIN (daily_typing_pages) |
+-------------------------+
|           |
+-------------------------+
1 row in Set (0.00 sec)

You can find the minimum values for all records by using the group by name as follows:

Mysql> ID, name, work_date, MIN (daily_typing_pages)
  -> from Employee_tbl GROUP by name;
+------+------+------------+-------------------------+
| id |  name | work_date | MIN (daily_typing_pages) |
+------+------+------------+-------------------------+
|  3 | Jack | 2007-05-06 |           |
|  4 | Jill | 2007-04-06 |           The |
|  1 | John | 2007-01-24 |           The |
|  2 | Ram | 2007-05-27 |           The |
|  5 | Zara | 2007-06-06 |           |
+------+------+------------+-------------------------+
5 rows in Set (0.00 sec)

You can use the Min function with the Max function to find the minimum value. Try the following example:

Mysql> SELECT MIN (daily_typing_pages) least, Max (daily_typing_pages) Max
  -> from Employee_tbl;
+-------+------+
| least | max |
+-------+------+
|  100 | |
+-------+------+
1 row in Set (0.01 sec)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.