Home > Database > Mysql Tutorial > body text

The difference between MIN() and LEAST() in MySQL

藏色散人
Release: 2019-04-08 09:20:54
Original
6817 people have browsed it

In each case, the two functions perform similar operations, but they have different syntaxes. (Related recommendations: "MySQL Tutorial")

Grammar

The syntax of MIN() and LEAST() are:

MIN([DISTINCT] expr) [over_clause]
LEAST(value1,value2,...)
Copy after login

So the MIN() function accepts different keywords and OVER clauses (while the LEAST() function does not).

The main difference between these two functions is the parameters they accept.

Specifically:

1.MIN() receives one parameter

2.LEAST() receives multiple parameters

So MIN() usually Returns the minimum value in a column in the database. The table can contain many rows, but this function returns the row with the smallest value.

LEAST(), on the other hand, returns the minimum argument from the argument list passed to it. You can pass three parameters to this function and it will return the one with the smallest value.

Example 1 - MIN() function

SELECT MIN(Population) AS 'Result'
FROM City;
Copy after login

Result:

+--------+
| Result |
+--------+
|     42 |
+--------+
Copy after login

This example finds the city with the least population from the city table. The column containing the population of each city is called population.

The point of this example is that only one parameter is provided to the function, but multiple rows are queried.

If you try to pass multiple parameters to the MIN() function, you will get an error.

Example 2 - LEAST() function

SELECT LEAST(1, 5, 9) AS 'Result';
Copy after login

Result:

+--------+
| Result |
+--------+
|      1 |
+--------+
Copy after login

In this example, we provided three parameters. Each parameter is compared with another parameter. This is in contrast to the single argument provided to the MIN() function.

If you try to pass an argument to the LEAST() function, you will get an error.

This article is about the difference between MIN() and LEAST() in MySQL. I hope it will be helpful to friends in need!

The above is the detailed content of The difference between MIN() and LEAST() in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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