Is there a way to retrieve the minimum value of fields in MySQL?


Yes, you can 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;

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;

This will produce the following output −

+--------------------------+
| least(Date1,Date2,Date3) |
+--------------------------+
| 2019-01-01               |
+--------------------------+
1 row in set (0.04 sec)
raja
Published on 11-Jul-2019 11:37:17
Advertisements