
- 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 records with a particular date and time?
You can use BETWEEN clause from MySQL to select records with a particular date and time. The syntax is as follows.
select *from AllRecordsFromadate where AdmissionDate between 'yourDateTimeValue1 ' and ''yourDateTimeValue2';
To understand the above syntax, let us first create a table. The query to create a table is as follows.
mysql> create table AllRecordsFromadate -> ( -> Id int, -> Name varchar(100), -> Age int, -> AdmissionDate datetime -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command. The query to insert records is as follows.
mysql> insert into AllRecordsFromadate values(101,'John',23,'2018-10-13'); Query OK, 1 row affected (0.18 sec) mysql> insert into AllRecordsFromadate values(102,'Carol',24,'2014-12-5 12:34:50'); Query OK, 1 row affected (0.18 sec) mysql> insert into AllRecordsFromadate values(103,'Mike',25,'2014-12-5 12:30:40'); Query OK, 1 row affected (0.23 sec) mysql> insert into AllRecordsFromadate values(104,'Bob',24,'2015-10-7 11:10:20'); Query OK, 1 row affected (0.20 sec) mysql> insert into AllRecordsFromadate values(105,'Sam',25,'2011-6-26 10:10:20'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select command. The query is as follows.
mysql> select *from AllRecordsFromadate;
The following is the output.
+------+-------+------+---------------------+ | Id | Name | Age | AdmissionDate | +------+-------+------+---------------------+ | 101 | John | 23 | 2018-10-13 00:00:00 | | 102 | Carol | 24 | 2014-12-05 12:34:50 | | 103 | Mike | 25 | 2014-12-05 12:30:40 | | 104 | Bob | 24 | 2015-10-07 11:10:20 | | 105 | Sam | 25 | 2011-06-26 10:10:20 | +------+-------+------+---------------------+ 5 rows in set (0.00 sec)
The following is the query can be used for specific date and time.
mysql> select *from AllRecordsFromadate where AdmissionDate between '2014-12-05 10:00:00' and '2014-12-05 12:50:58';
The following is the output.
+------+-------+------+---------------------+ | Id | Name | Age | AdmissionDate | +------+-------+------+---------------------+ | 102 | Carol | 24 | 2014-12-05 12:34:50 | | 103 | Mike | 25 | 2014-12-05 12:30:40 | +------+-------+------+---------------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to select records with a particular date?
- MySQL SELECT query to return records with specific month and year
- MySQL query to subtract date records with week day and display the weekday with records
- MySQL query to fetch the latest date from a table with date records
- MySQL query to select records from a table on the basis of a particular month number?
- Display records after a particular date in MySQL
- MySQL query to select top 10 records?
- MySQL query to select bottom n records
- MySQL time period query to fetch date records from interval of 14 weeks from current date?
- MySQL query to select date >= current date - 3 weeks?
- MySQL query to select all entries from a particular month
- MySQL query to select date from timestamp?
- MySQL query to find the date records wherein the current date and time is in between the JoiningDate and RelievingDate
- MySQL query to select records beginning from a specific id
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?

Advertisements