
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to select closest date from today?
Let’s say the current date is 2019-07-25. We will now see an example and create a table where ShippingDate is added in the table.
Let us first create a table −
mysql> create table DemoTable667(ShippingDate datetime); Query OK, 0 rows affected (0.46 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable667 values('2019-01-31'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable667 values('2019-07-19'); Query OK, 1 row affected (0.69 sec) mysql> insert into DemoTable667 values('2019-07-23'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable667 values('2019-08-24'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable667;
This will produce the following output −
+---------------------+ | ShippingDate | +---------------------+ | 2019-01-31 00:00:00 | | 2019-07-19 00:00:00 | | 2019-07-23 00:00:00 | | 2019-08-24 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
Following is the query to get the closest date from today −
mysql> select ShippingDate from DemoTable667 where date(ShippingDate) = (select min(date(ShippingDate)) from DemoTable667 where date(ShippingDate) > date(now()) );
This will produce the following output −
+---------------------+ | ShippingDate | +---------------------+ | 2019-07-23 00:00:00 | +---------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to select date from timestamp?
- MySQL query to select date from 00:00 to today’s date
- MySQL Select Date Equal to Today and return results for the same date?
- MySQL query to select date >= current date - 3 weeks?
- SELECT MySQL rows where today's date is between two DATE columns?
- Select all rows except from today in MySQL?
- MySQL query to select records with a particular date?
- MySQL date format DD/MM/YYYY select query?
- MySQL - Select dates that are one week ahead from today?
- Format date in MySQL SELECT * query uisng FORMATDATE() method?
- How to select date from timestamp in MySQL?
- Select date from MySQL and format to text?
- MySQL Query to convert from datetime to date?
- MySQL query to select records with a particular date and time?
- Get date format DD/MM/YYYY with MySQL Select Query?

Advertisements