Tutorialspoint

Fetch a single ordered date from a column with MySQL LIMIT


1 Answer
Sharon Christine

To fetch a single date from a column, use “LIMIT 1. To order it, use ORDER BY clause. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> DueDate varchar(100)
   -> );
Query OK, 0 rows affected (1.16 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('10-06-2019');
Query OK, 1 row affected (0.42 sec)

mysql> insert into DemoTable values('01-12-2016');
Query OK, 1 row affected (0.52 sec)

mysql> insert into DemoTable values('31-01-2018');
Query OK, 1 row affected (0.58 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 10-06-2019 |
| 01-12-2016 |
| 31-01-2018 |
+------------+
3 rows in set (0.00 sec)

Following is the query to fetch a single ordered date from a column with MySQL LIMIT −

mysql> select *from DemoTable ORDER BY STR_TO_DATE(DueDate, '%d-%m-%Y') LIMIT 1;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 01-12-2016 |
+------------+
1 row in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.