Home  >  Article  >  Database  >  The difference between MIN() and LEAST() in MySQL

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

藏色散人
藏色散人Original
2019-04-08 09:20:036816browse

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,...)

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;

Result:

+--------+
| Result |
+--------+
|     42 |
+--------+

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';

Result:

+--------+
| Result |
+--------+
|      1 |
+--------+

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!

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:MySql index thingsNext article:MySql index things