Syntax error How to find the minimum values of two or more fields in MySQL?

How to find the minimum values of two or more fields in MySQL?



To find the minimum values of two or more fields, use LEAST() function from MySQL −

select least(yourColumnName1,yourColumnName2,...N) from yourTableName;

Let us first create a table −

mysql> create table DemoTable
-> (
-> Date1 date,
-> Date2 date,
-> Date3 date
-> );
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-03-31','2019-01-01','2019-03-05');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------------+------------+------------+
| Date1      | Date2      | Date3      |
+------------+------------+------------+
| 2019-03-31 | 2019-01-01 | 2019-03-05 |
+------------+------------+------------+
1 row in set (0.00 sec)

Following is the query to retrieve the minimum value in MySQL −

mysql> select least(Date1,Date2,Date3) from DemoTable;

Output

This will produce the following output −

+--------------------------+
| least(Date1,Date2,Date3) |
+--------------------------+
| 2019-01-01               |
+--------------------------+
1 row in set (0.04 sec)
Updated on: 2020-06-30T12:54:44+05:30

844 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements